Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created June 21, 2023 10:45
Show Gist options
  • Save Ifihan/59299c7d73be2288abf8b9262f6ee650 to your computer and use it in GitHub Desktop.
Save Ifihan/59299c7d73be2288abf8b9262f6ee650 to your computer and use it in GitHub Desktop.
Is Subsequence

Is Subsequence

Question on Leetcode - Easy

Approach

The approach taken was to use a pointer intialized at 0, for the sequence s string. This pointer servers as a counter which the increases for every positive match against the main string. Afterwards, I traversed through the main string t and returned the a boolean of if the pointer value was the same as the length of the sequence s.

Code

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

Complexities

  • Time Complexity: O(n) for the length of the main string.
  • Space complexity: O(1) constant space as nothig is really added apart from the pointer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment