53. Is it a subsequence?
A subsequence of a string is what is left after deleting some of its characters (or none), keeping the rest in their
order. "ace" is a subsequence of "abcde": delete b and d. "aec" isn't, because in "abcde" the c comes before
the e.
Write a function is_subsequence(small, big) that takes two strings and returns True if small is a subsequence of
big, or False if it isn't.
- Every character of
smallmust be matched by a different character ofbig:"aa"is not a subsequence of"a". - The empty string is a subsequence of every string, the empty one included.
Example 1
small = "ace", big = "abcde"TrueExample 2
small = "aec", big = "abcde"FalseExample 3
small = "", big = "abc"TrueConstraints
smallhas 0 to 50 andbig0 to 200 lower-case letters.
Submit also runs 9 more tests you can't see.
Practises While Loops (lesson 14).
- Keep two indexes,
iinsmallandjinbig, both starting at 0, and loop while both are in range. jmoves on every step.imoves only whensmall[i] == big[j]: that letter is found, so look for the next one.- When the loop ends,
smallis a subsequence exactly wheni == len(small). Checkingch in bigfor each letter, orsmall in big, ignores the order or the gaps.
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
def is_subsequence(small, big):
"""Check whether small's letters appear in big in order.
small: str, the letters to find; may be empty
big: str, the string to find them in; may be empty
Returns: bool, True if small is a subsequence of big
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.