Algorithm/Leetcode

[Top Interview 150 - Two Pointers] 392. Is Subsequence

code-bean 2024. 10. 12. 17:36
[Q] [Easy]

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

A subsequence of a string is a new string that is formed 
from the original string by deleting some (can be none) of the characters 
without disturbing the relative positions of the remaining characters. 
(i.e., "ace" is a subsequence of "abcde" while "aec" is not).

 

  • 이런 문제가 취약점인가보다 좀 헷갈려서 찾아보고 풀었다.

 

# Answer

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:

        # len(s) == 0
        if len(s) == 0:
            return True

        # s와 t 비교
        s_index = 0
        t_index = 0

        while s_index < len(s) and t_index < len(t):
            if s[s_index] == t[t_index]:
                s_index += 1
            t_index += 1

        return s_index == len(s)