-
Notifications
You must be signed in to change notification settings - Fork 0
392, Is Subsequence #16
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
nittoco
wants to merge
1
commit into
main
Choose a base branch
from
nittoco-patch-16
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,201 @@ | ||
| ## Step1 | ||
|
|
||
| - 一発目のコード。間違い。 | ||
| - 普通の処理の時のt_indexの進め忘れ。ただ、t_indexを進めると今度はインデックスの数字がオーバーするので調整が必要になり、ちょっと時間を食った。 | ||
|
|
||
| ```python | ||
| python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| t_index = 0 | ||
| for s_index in range(len(s)): | ||
| while t[t_index] != s[s_index]: | ||
| t_index += 1 | ||
| if t_index >= len(t): | ||
| return False | ||
| return True | ||
|
|
||
| ``` | ||
|
|
||
| - 上の調整を終えた後のコード、t_indexがlenを超えたらFalse、の判定を2箇所に分けたくなかったので、ifのインデントなど工夫した | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| t_index = 0 | ||
| for s_index in range(len(s)): | ||
| while t_index < len(t) and t[t_index] != s[s_index]: | ||
| t_index += 1 | ||
| if t_index >= len(t): | ||
| return False | ||
| t_index += 1 | ||
| return True | ||
|
|
||
| ``` | ||
|
|
||
| ## Step2 | ||
|
|
||
| - まずこれ(https://discord.com/channels/1084280443945353267/1225849404037009609/1243290893671465080) が目に付く。すごい選択肢の量。 | ||
| - 関数型はよく知らないが、再帰でループを書くのね。最終的な条件がシンプルになるので、綺麗な感じはします。 | ||
| - for文にするかwhile文にするかは考えていたが、以下の選択肢はあんまり考えてなかった | ||
|
|
||
|
|
||
| - while Trueにする | ||
| - 最初は空文字列の処理を忘れ一敗(下のは直した後) | ||
| - 関数型に近いと感じる。これ結構好きかも | ||
|
|
||
| ```python | ||
| python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| if len(s) == 0: | ||
| return True | ||
| if len(t) == 0: | ||
| return False | ||
| si = 0 | ||
| ti = 0 | ||
| while True: | ||
| if s[si] == t[ti]: | ||
| si += 1 | ||
| ti += 1 | ||
| if si == len(s): | ||
| return True | ||
| if ti == len(t): | ||
| return False | ||
| ``` | ||
|
|
||
| - 上のやつ、odaさんの見たらうまく条件変えて0の判定せずに済んでる(自分で気づかなかった) 一般に、indexを参照する直前に判定した方が簡潔になりやすいのかな? | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| si = 0 | ||
| ti = 0 | ||
| while True: | ||
| if si == len(s): | ||
| return True | ||
| if ti == len(t): | ||
| return False | ||
| if s[si] == t[ti]: | ||
| si += 1 | ||
| ti += 1 | ||
| ``` | ||
|
|
||
| - 1万外側のループをtにする、今度は下のように書いて1敗 | ||
| - tの最後の文字==sの最後の文字 の処理が走る場合に部分文字列があるのに検出できず(すぐ気づいた) | ||
| - 今回は残念ながら空文字のTrue返す処理を分けるしかなさそう。綺麗じゃないけど。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| si = 0 | ||
| for ti in range(len(t)): | ||
| if si == len(s): | ||
| return True | ||
| if s[si] == t[ti]: | ||
| si += 1 | ||
| return False | ||
|
|
||
| ``` | ||
|
|
||
| - 訂正後のコード | ||
| - 自分は、「sの文字を1個ずつ追っていってt側にあるかな?と順に確かめる」という気持ちなので、あんまりtを外側に出す気持ちではないかも | ||
|
|
||
| ```python | ||
| python | ||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| if len(s) == 0: | ||
| return True | ||
| si = 0 | ||
| ti = 0 | ||
| for ti in range(len(t)): | ||
| if s[si] == t[ti]: | ||
| si += 1 | ||
| if si == len(s): | ||
| return True | ||
| return False | ||
| ``` | ||
|
|
||
|
|
||
| - while elseを使う(なんとなく聞いたことあるくらいで、よく把握していないので調べた) これは全然慣れない。結局コードを参照しながら書いて時間がかかった。慣れといた方がいいのか? | ||
| - breakした時の処理(今回でいうsi+=1 ti+=1の処理)が長い時には手段の1つではあるが、他の手段もあるし…(https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860) | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| si = 0 | ||
| ti = 0 | ||
| while si < len(s): | ||
| while ti < len(t): | ||
| if s[si] == t[ti]: | ||
| break | ||
| ti += 1 | ||
| else: | ||
| return False | ||
| si += 1 | ||
| ti += 1 | ||
| return True | ||
| ``` | ||
|
|
||
| - 正規表現を使う | ||
| - 何回か計測したが、かなり遅いみたい | ||
| - わかりやすくはある | ||
|
|
||
| ```python | ||
|
|
||
| import re | ||
|
|
||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| regrex = [] | ||
| for sc in s: | ||
| regrex.append(f'.*{sc}') | ||
| regrex = ''.join(regrex) | ||
| return re.match(regrex, t) | ||
| ``` | ||
|
|
||
| - これくらいなら、変数名はsi, tiでいいね | ||
| - sを外にあるコードもDiscordにあるodaさんのやつ(下のやつ、([https://discord.com/channels/1084280443945353267/1225849404037009609/1243290893671465080](https://discord.com/channels/1084280443945353267/1225849404037009609/1243304635998011615))の1番目のコード)の方が簡潔にかける。これもでも結局、tの方を必ず1進める、sを進めるかは場合によって、という処理 | ||
| - for文でsをイテレートするという気持ちになってしまうとこの選択肢は取れないので、難しい | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| si = 0 | ||
| ti = 0 | ||
| while si < len(s): | ||
| if ti >= len(t): | ||
| return False | ||
| if s[si] == t[ti]: | ||
| si += 1 | ||
| ti += 1 | ||
| return True | ||
| ``` | ||
|
|
||
| - この辺も一通り見た | ||
| - return s[i]==len(s) みたいにする選択肢もあると気づいた | ||
| - https://github.com/hayashi-ay/leetcode/pull/64/files | ||
| - https://github.com/shining-ai/leetcode/pull/57/files | ||
|
|
||
| ## Step3 | ||
|
|
||
| - これで書いた | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| si = 0 | ||
| ti = 0 | ||
| while si < len(s): | ||
| if ti == len(t): | ||
| return False | ||
| if s[si] == t[ti]: | ||
| si += 1 | ||
| ti += 1 | ||
| return True | ||
| ``` | ||
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.
私も、Python native じゃないので、while else 慣れない(し自然さは感覚がない)んですが、CPython のコードなどを見ていると、たまに出てきますね。
https://github.com/python/cpython/blob/2e0aa731aebb8ef3d89ada82f5d39b1bbac65d1f/Modules/itertoolsmodule.c#L2625
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.
コード例ありがとうございます。
見てみましたが、自分にとっては複雑でまだ処理が終えてないのでまた時間がある時に見てみます