Intuition

  • Check if the current character == the one you are at. If so, increase i by 1.
  • When i reaches the end of s, then you have encountered an occurrence of each string you wish for in s, and it has occurred in t.

Runtime

to go through each character in T

Code

Alternative 2

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        if not s:
            return True
 
        i = 0
        j = 0
 
        for j in range(len(t)):
            if i < len(s) and s[i] == t[j]:
                i += 1
 
        if i == len(s):
            return True
        return False

Alternative 1

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        i, s_len = 0, len(s)
        for j in range(len(t)):
            if i == s_len:
                return True
            if s[i] == t[j]:
                i += 1
 
        return i == s_len

References

https://leetcode.com/problems/is-subsequence/