Input으로 주어진 s가 또 다른 input인 t의 subsequence(연속이 아니더라도 포함이 돼있다면)인지 True/False를 반환하면 된다. 순서를 지켜야하기 때문에 sorting이나 hash map을 사용하는 것이 아니라 index를 이용해 비교해 나가는 방식으로 풀었다.
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:
if s == "":
return True
l = r = 0
while r < len(t):
if s[l] == t[r]:
l += 1
r += 1
if l == len(s):
return True
return False
참고
'기술(Tech, IT) > 리트코드(LeetCode)' 카테고리의 다른 글
[LeetCode] 1165. Single-Row Keyboard (0) | 2023.07.19 |
---|---|
[LeetCode] 1151. Minimum Swaps to Group All 1's Together (0) | 2023.07.18 |
[LeetCode] 229. Majority Element II (0) | 2023.07.13 |
[LeetCode] 169. Majority Element (0) | 2023.07.12 |
[LeetCode] 70. Climbing Stairs (0) | 2023.07.11 |