Question on Leetcode - Easy
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
.
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)
- 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.