50 XP

53. Is it a subsequence?

Intermediate two pointers strings

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 small must be matched by a different character of big: "aa" is not a subsequence of "a".
  • The empty string is a subsequence of every string, the empty one included.

Example 1

Inputsmall = "ace", big = "abcde"
OutputTrue
Explanationa, c and e appear in that order.

Example 2

Inputsmall = "aec", big = "abcde"
OutputFalse
ExplanationThe only c comes before the e.

Example 3

Inputsmall = "", big = "abc"
OutputTrue
ExplanationDelete everything: nothing is left.

Constraints

  • small has 0 to 50 and big 0 to 200 lower-case letters.

Submit also runs 9 more tests you can't see.

Practises While Loops (lesson 14).

Code
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