-
Notifications
You must be signed in to change notification settings - Fork 0
3. Longest Substring Without Repeating Characters #50
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
base: main
Are you sure you want to change the base?
Conversation
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| start = 0 | ||
| stop = 1 |
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.
stop より last の方が実態を表していてより適切と書籍(リーダブルコード)で読んだことがあります。
| for left in range(s_len): | ||
| for right in range(left, s_len): |
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.
自分なら文字ではなくインデックスであることを強調するために left_i 、 right_i としますね。
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.
#35 (comment)
このようなコメントをいただいた事があり、以降消すようにしています。
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.
ハンガリアン記法的な話ですね
| return max_len | ||
| ``` | ||
| 思考ログ: | ||
| - ややテクニカルだが効率はいいなと感じた |
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.
文字とインデックスの辞書を作成してそれを参照する方法なので、個人的にはそこまでテクニカルではなくオーソドックスな方法だと思いました。
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.
自分には、最後に見たインデックス情報を活用するこの方法がすぐ出てきませんでした。。
現在のleftより前に戻らないようにする部分も初手でハンドリングできていたか怪しいです。
精進致します。
| substring_chars = set() | ||
| longest_substring_len = 0 | ||
| while stop <= len(s): | ||
| c = s[stop - 1] |
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.
好みの範疇かもしれませんが、stop (last) のほうが、それを含まないインデックスであるというのは少し違和感があります。
二分探索だと右端を含むか含まないかというのはむしろ自由に書き換えられたほうがよいとは思うのですが、ここは文字列のそのインデックスにアクセスしているので、s[stop] になるように書くのが自然な印象です (そうすると長さを求める部分は stop - start + 1 になりますね)。
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.
私もこっちのほうが好みです。
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.
こういうコメントを頂いた背景があります。
#38 (comment)
| for left in range(s_len): | ||
| for right in range(left, s_len): |
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.
ハンガリアン記法的な話ですね
| longest_substring_len = 0 | ||
| for left in range(s_len): | ||
| for right in range(left, s_len): | ||
| sub_s = s[left:right + 1] |
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 | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| letter_to_count = {c: 0 for c in s} |
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.
defaultdict とか Counter を使いたくなります
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| letter_to_count = {c: 0 for c in s} | ||
|
|
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.
あまり意味のない空行のように思います
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.
意味がないですね。
ありがとうございます。
| ``` | ||
| 思考ログ: | ||
| - ややテクニカルだが効率はいいなと感じた | ||
| - leftを更新する際に現在のleftより前に戻らないようにすることを注意しないといけない |
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.
私もハマりました
|
|
||
| # Step3 | ||
|
|
||
| かかった時間:4min |
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.
これ 3 回やって 4 min ですかね?突っ込むほどのこともないですが、もうちょっと早くなる気もしており、いや別に早くすることが目的ではないんですが、なにか迷いがあるのかなあ、と思いました。
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.
確かにと思い、解き直してみましたが、2分程かかりました。
割と解きながら他の選択肢について思い出していたりすることがあるので、それで余計な時間を使ったのかもしれません。
| letter_to_count[s[right]] += 1 | ||
| max_len = max(max_len, right - left + 1) |
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.
細かいですが、自分ならこの2つの行逆にします。(for文の最後でrightが進むことに関係する部分は動かしたいため)
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.
確かにそうですね。
忘れそうだと思って先に書いてしまったんですかね。
| if s[right] in letter_to_last_index: | ||
| left = max(left, letter_to_last_index[s[right]] + 1) | ||
|
|
||
| max_len = max(max_len, right - left + 1) |
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.
最大の長さであることはは読んでいれば割とすぐわかる情報で、むしろresultの方が何についての最大の長さなのか関数名を見ればわかるので(関数名がはっきりしていたら)、個人的には好みです
| for left in range(s_len): | ||
| for right in range(left, s_len): |
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.
s_lenと変数を作るより普通にlen(s)のままの方がわかりやすく思います
|
|
||
| 文字列の長さをNとして | ||
| - 時間計算量:O(N) | ||
| - 空間計算量:O(N) |
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.
かなり前のPRへのコメントで恐縮ですが、この問題の制約に
sconsists of English letters, digits, symbols and spaces.
とあるので、入力の文字列がめちゃめちゃ大きくなったとしても、substringはあるサイズで頭打ちになる -> 空間計算量はO(1)な気がします。
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/