Perl 5, 17 bytes (+1?), full program
s//.*/g;$_=<>=~$_
Try it online!
Invoke with the p
flag to the perl interpreter, as in perl -pe 's//.*/g;$_=<>=~$_'
. Per the established scoring rules when this challenge was originally posted, this flag costs one extra byte. Under more recent rules, AFAICT, it may be free.
Either way, the input strings should be supplied on separate newline-terminated lines on stdin. Output (to stdout) will be 1
if the first input string is a substring of the second, or nothing at all if it's not.
Note that both input lines must have a newline at the end, or the program won't work correctly. Alternatively, you can add the l
command line flag to the invocation to make perl strip the newlines; depending on the scoring rules in effect, this may or may not cost one extra byte. Note that using this flag will also append a newline to the output.
Original version (snippet, 18 bytes / chars)
$x=~s//.*/g,$y=~$x
Input is given in the variables $x
and $y
, result is the value of the expression (in scalar context). Note that $x
is modified in the process.
(Yes, I know using $_
instead of $x
would let me save four chars, but doing that in a snippet that feels a bit too cheesy for me.)
How does it work?
The first part, $x=~s//.*/g
, inserts the string .*
between each character in $x
. The second part, $y=~$x
, treats $x
as a regexp and matches $y
against it. In Perl regexps, .*
matches zero or more arbitrary characters, while all alphanumeric characters conveniently match themselves.
anna
is a subsequence (but not a substring) ofbanana
. String X is a subsequence of string Y just if X can be obtained from Y by deleting zero or more of the elements of Y; e.g., deleting theb
and the seconda
frombanana
givesanna
. \$\endgroup\$