392. Is Subsequence

Numeric_combo·2024년 11월 24일

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).

Example 1:

Input: s = "abc", t = "ahbgdc"
Output: true
Example 2:

Input: s = "axc", t = "ahbgdc"
Output: false

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        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)

투포인터 쓰는 거는 알았는데 s와 t를 어떻게 비교하면서 하는 지가 당최 떠오르지가 않았다 ㅠ..생각보다 간단한 구현이어서 오늘도 나의 모자람과 멍충함을 께달음.

profile
덕질기록용

0개의 댓글