Is subsequence

October 6, 2022

two-pointers

Problem URL: Is subsequence

We will take two pointers, and then go through both string and increase the number of the pointers. When we exit the loop, if the pointer value of the first pointer is equal to the lenght of the first sting, we return true otherwise false.

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

Time Complexity: O(n)
Space Complexity: O(1)