-
Notifications
You must be signed in to change notification settings - Fork 0
Create Is Subsequence.md #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yakataN
wants to merge
1
commit into
main
Choose a base branch
from
Is-Subsequence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| ### Is Subsequence | ||
|
|
||
| https://leetcode.com/problems/is-subsequence | ||
|
|
||
| #### step1 | ||
| ```python3 | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| # 先頭からみて同じものがあれば消費する。最後までindexを回せたら終わり | ||
| # 終了判定を挟む必要があり、そこをわかりやすくかけるかどうかでindexの扱いを変えたい | ||
| if len(s) == 0: | ||
| return True | ||
| seen = 0 | ||
| for c in t: | ||
| if s[seen] == c: | ||
| seen += 1 | ||
| if seen == len(s): | ||
| return True | ||
| return False | ||
| ``` | ||
|
|
||
| #### step2 | ||
| [Arai60/392. Is Subsequence.md at 0aac26e68fa6b8f516ff494da34578b8cc1cefc2 · olsen-blue/Arai60 · GitHub](https://github.com/olsen-blue/Arai60/blob/0aac26e68fa6b8f516ff494da34578b8cc1cefc2/392.%20Is%20Subsequence.md#%E8%A7%A3%E6%B3%952%E4%BB%95%E4%BA%8B%E3%81%AE%E5%BC%95%E3%81%8D%E7%B6%99%E3%81%8E%E3%83%88%E3%83%83%E3%83%97%E3%83%80%E3%82%A6%E3%83%B3%E5%86%8D%E5%B8%B0dpac) | ||
| 再帰で書くループ | ||
| やっていることはstep1と同じだが、再帰苦手なので、こっちでも実装する | ||
|
|
||
| ```python3 | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| def traverse(s_index, t_index) -> bool: | ||
| if s_index == len(s): | ||
| return True | ||
| if t_index == len(t): | ||
| return False | ||
|
|
||
| if s[s_index] == t[t_index]: | ||
| return traverse(s_index + 1, t_index + 1) | ||
| else: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 再帰スッキリかけていて読みやすかったです。 |
||
| return traverse(s_index, t_index + 1) | ||
| return traverse(0, 0) | ||
| ``` | ||
| return traverse(s_index + 1, t_index + 1)のreturnをつけ忘れて1敗 | ||
|
|
||
| 再帰をwhile True:で書き直す方法も真似る | ||
| ```python3 | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| s_index = 0 | ||
| t_index = 0 | ||
|
|
||
| while True: | ||
| if s_index == len(s): | ||
| return True | ||
| if t_index == len(t): | ||
| return False | ||
|
|
||
| if s[s_index] == t[t_index]: | ||
| s_index += 1 | ||
| t_index += 1 | ||
| else: | ||
| t_index += 1 | ||
| ``` | ||
|
|
||
|
|
||
| [392. Is Subsequence by fhiyo · Pull Request #55 · fhiyo/leetcode · GitHub](https://github.com/fhiyo/leetcode/pull/55/files#diff-a6c7d5ff748fd033529b0b0a550ed2aa570e18edc3e2c61da5094aec0e23a91eR45-R46) | ||
| - 二分探索法 | ||
| - s<<tでかつ、sが違う処理が入るときにtを前処理して置きたいという気持ちから | ||
| - 計算量はO(len(s)loglen(t))でO(len(t))よりは短くなる状況はありそう。 | ||
|
|
||
| #### step3 | ||
| ```python3 | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| if s == "": | ||
| return True | ||
|
|
||
| seen = 0 | ||
| for char_t in t: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if s[seen] == char_t: | ||
| seen += 1 | ||
| if seen == len(s): | ||
| return True | ||
| return False | ||
| ``` | ||
|
|
||
| 1:14, 57, 56sec | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seen という変数名から、どのような値が格納されているのか分かりづらく感じました。 step2 の s_index のほうが分かりやすく感じました。