ほぼ日刊競プロ leetcode 392. Is Subsequence
392. Is Subsequence
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).
考えたこと
文字列sが文字列tに順番に通りに含まれているかを判定する.
文字列tの数だけfor文を回し,文字列sを省いていく.
文字列sが0になればTrue,そうでなければFalseとする
s,tともに空文字というパターンもあるので最初のif分を追加している.
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) == 0 and len(t) == 0:
return True
for i in range(len(t)):
print (t[i])
if t[i] in s and len(s)!=0:
s=s.removeprefix(t[i])
print (s)
if len(s)==0:
return True
if len(s)!=0:
return False