Skip to content
Open
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
11 changes: 11 additions & 0 deletions arai60/is_subsequence/phase1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) == 0:
return True
cursor_of_s = 0
for char_of_t in t:
if char_of_t == s[cursor_of_s]:
cursor_of_s += 1
if cursor_of_s >= len(s):
return True
return False
30 changes: 30 additions & 0 deletions arai60/is_subsequence/phase2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Reference
go-to untrappedさん: https://github.com/goto-untrapped/Arai60/pull/19/files 正規表現の実装を一回はやってみます
shining-aiさん: https://github.com/shining-ai/leetcode/pull/57/files
昔読んだ自然言語処理の本で現在のポインターの指すところをポインターだとかカーソルだとか命名していたのでカーソルって命名している癖が抜けてないので修正


問題は解けたが, なんだかDFAとかなんとか教育的なことを話しているぞ...?
https://news.ycombinator.com/item?id=37422355
while文で回している人が多かったが, t_indexはどのみち1増えていくので, for文で実装する方針にした。
"""
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) == 0:
return True
s_index = 0
for c in t:
if c == s[s_index]:
s_index += 1
if s_index >= len(s):
return True
return False

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイルの終端は改行で終えましょう。

11 changes: 11 additions & 0 deletions arai60/is_subsequence/phase3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) == 0:
return True
s_index = 0
for c in t:
if c == s[s_index]:
s_index += 1
if s_index >= len(s):
return True
return False