https://leetcode.com/problems/is-subsequence/
Is Subsequence - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Subsequence는 어떤 String에서 임의의 문자들을 삭제했을때 만들어지는 String이라고 합니다.
s가 t의 Subsequence인지 묻는 문제입니다.
문제 자체는 그리디한 느낌으로 s의 문자들을 차례대로 t에서 찾으면 해결됩니다.
다만 제 풀이코드 기준에서 s와 t의 길이에 따라 index가 범위를 넘어가는 경우가 있어서 예외처리해 주었습니다.
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if(len(s) > len(t)):
return False
if(len(s) == 0):
return True
idx = 0
for i in t:
if(i == s[idx]):
idx += 1
if(len(s) == idx):
return True
return False
input = [["abc", "ahbgdc"],["axc", "ahbgdc"]]
for i in input:
print(Solution().isSubsequence(i[0],i[1]))
'🔍 알고리즘 > Leetcode' 카테고리의 다른 글
[Python] Leetcode 1137. N-th Tribonacci Number(Easy) (0) | 2022.07.22 |
---|---|
[Python] Leetcode 509. Fibonacci Number (Easy) (0) | 2022.07.22 |
[Python] Leetcode 205. Isomorphic Strings (Easy) (0) | 2022.07.22 |
[Python] Leetcode 724. Find Pivot Index (Easy) (0) | 2022.07.21 |
[Python] Leetcode 1480. Running Sum of 1d Array (Easy) (0) | 2022.07.21 |