Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions arai60/54-60_others/57_392_Is Subsequence/level_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ポインタを2つ用意して、同じ文字ならsを次に進める
# sが最後まで進めばSubsequenceが存在する
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
t_index = 0
for s_index in range(len(s)):
if len(t) <= t_index:
return False
while t[t_index] != s[s_index]:
t_index += 1
if len(t) == t_index:
return False
t_index += 1
return True
30 changes: 30 additions & 0 deletions arai60/54-60_others/57_392_Is Subsequence/level_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ポインタを2つ用意して、同じ文字ならsを次に進める
# while文で記載
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
t_index = 0
s_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)


# DPで最長共通部分列問題を解く
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
# longest_common_nums[i][j]はs[:i]とt[:j]の最長共通部分列の長さ
longest_common_nums = [[0] * (len(t) + 1) for _ in range(len(s) + 1)]
for i in range(len(s)):
for j in range(len(t)):
if s[i] == t[j]:
longest_common_nums[i + 1][j + 1] = (
longest_common_nums[i][j] + 1
)
continue
longest_common_nums[i + 1][j + 1] = max(
longest_common_nums[i + 1][j],
longest_common_nums[i][j + 1],
)
return longest_common_nums[-1][-1] == len(s)
9 changes: 9 additions & 0 deletions arai60/54-60_others/57_392_Is Subsequence/level_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
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)
22 changes: 22 additions & 0 deletions arai60/54-60_others/57_392_Is Subsequence/level_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ポインタを2つ用意して、同じ文字ならsを次に進める
# sが最後まで進めばSubsequenceが存在する
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
t_index = 0
for s_index in range(len(s)):
for t_index in range(t_index, len(t)):
if t[t_index] == s[s_index]:
t_index += 1
break
else:
return False
return True


# 正規表現
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
pattern = ""
for c in s:
pattern += ".*" + c
return re.match(pattern, t) != None