Is Subsequence LeetCode Challenge June Day 9



Quoestion: ClickHere
Example 1:
Input: s = "abc", t = "ahbgdc"
Output: true

Example 2:
Input: s = "cdo", t = "CodeToSkill"
Output: false
Algorithm:
Step1:Check if s is Empty String or not if yes return True
Step2:Take take a  pointer as first element in s and intialize 
Step3:Traverse each character in t and check if pointer equals to that character or not if yes increase pointer 
Step4:if pointer reaches end of s return True
Step4:if any of character is not in t return False

Time Complexity: O(n) where n is length of string t

Python Code:

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




Post a Comment

0 Comments