[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)
'Algorithm > Leetcode' 카테고리의 다른 글
[Top Interview 150 - Hashmap] 383. Ransom Note (0) | 2024.10.12 |
---|---|
[Top Interview 150 - Two Pointers] 167. Two Sum II - Input Array Is Sorted (0) | 2024.10.12 |
[Top Interview 150 - Two Pointers] 125. Valid Palindrome (0) | 2024.10.12 |
[Top Interview 150 - Array / String] 189. Rotate Array (0) | 2024.10.12 |
[Top Interview 150 - Array / String] 169. Majority Element (0) | 2024.10.12 |