Logo
    Akhil Abraham
    Akhil Abraham
    0392 - Is Subsequence
    0392 - Is Subsequence

    0392 - Is Subsequence

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/is-subsequence/

    Solution 1 - Two Pointer

    Use two pointers: one for string s and one for string t. Iterate through t, and whenever the current character matches the current character in s, advance the s pointer. If the s pointer reaches the end of s, then s is a subsequence of t.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Iterator with all()

    Convert t into an iterator. For each character in s, use Python's in operator on the iterator, which consumes elements up to and including the match. The all() function returns True only if every character in s is found in order.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    """ 0392.1 - Is Subsequence - Solution 1 - Two Pointer """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isSubsequence(self, s: str, t: str) -> bool:
            """Is Subsequence Function"""
            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)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isSubsequence("abc", "ahbgdc"))   # True
        print(Solution().isSubsequence("axc", "ahbgdc"))   # False
        print(Solution().isSubsequence("", "ahbgdc"))      # True
        print(Solution().isSubsequence("abc", ""))          # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0392.2 - Is Subsequence - Solution 2 - Iterator with all() """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isSubsequence(self, s: str, t: str) -> bool:
            """Is Subsequence Function"""
            iterator = iter(t)
            return all(char in iterator for char in s)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isSubsequence("abc", "ahbgdc"))   # True
        print(Solution().isSubsequence("axc", "ahbgdc"))   # False
        print(Solution().isSubsequence("", "ahbgdc"))      # True
        print(Solution().isSubsequence("abc", ""))          # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()