-
Notifications
You must be signed in to change notification settings - Fork 0
Solve 3_longest_substring_without_repeating_characters_medium #9
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?
Solve 3_longest_substring_without_repeating_characters_medium #9
Conversation
| max_length = max(max_length, index - window_start_index + 1) | ||
| char_to_last_index[char] = index | ||
| return max_length | ||
|
|
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.
getを使うとこのように書けます。
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
start = 0
char_to_last_index = {}
max_length = 0
for i, c in enumerate(s):
start = max(start, char_to_last_index.get(c, -1) + 1)
max_length = max(max_length, i - start + 1)
char_to_last_index[c] = i
return max_lengthThere 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を使わずにgetだけで済むような場合もありそうですね、参考にさせていただきます。
| # 別解・模範解答 | ||
| ウィンドウ制御にsetを用いる方法もある。 | ||
|
|
||
| https://wiki.python.org/moin/TimeComplexity より、setの要素の追加・削除は平均して`O(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.
Setの実装を見ても良いかもしれません。
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.
ここらへんが実装だと思います。
https://github.com/python/cpython/blob/main/Objects/setobject.c
AIにcpythonのsetの実装箇所を聞くと答えてくれたりします。
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 0 | ||
|
|
||
| # character -> last index | ||
| char_to_last_index: dict[str, int] = defaultdict(int) |
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である必要はありますか?
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 char in char_to_last_index: が入っているので defaultdict である必要は無いですね、ご指摘ありがとうございます。
問題へのリンク
3. Longest Substring Without Repeating Characters
言語
Python
問題の概要
文字列
sが与えられたとき、重複しない文字からなる最長の部分文字列(substring)の長さを求める問題。自分の解法
文字列から文字
charを1文字ずつ取り出していく。文字charを走査しているとき、そこから前にたどって最長の部分文字列(で文字が重複していないもの)を管理しながら走査していく。各文字に対して、それが出現した最後のインデックスを記録するための辞書
char_to_last_indexを用意してウィンドウ制御を行う。時間計算量:
O(n)空間計算量:
O(1)step2
substring_start_index→window_start_index別解・模範解答
ウィンドウ制御にsetを用いる方法もある。
https://wiki.python.org/moin/TimeComplexity より、setの要素の追加・削除は平均して
O(1)であるため、以下のように書くことができる。O(n)O(1)次に解く問題の予告