-
Notifications
You must be signed in to change notification settings - Fork 0
Create 3. Longest Substring Without Repeating Characters.md #21
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
Merged
Mike0121
merged 2 commits into
main
from
3.-Longest-Substring-Without-Repeating-Characters
Nov 1, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
69 changes: 69 additions & 0 deletions
69
競技プロ就活部PR用/3. Longest Substring Without Repeating Characters.md
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,69 @@ | ||
| ## Sliding Window(set)による解法 ★ | ||
| --- | ||
| ### 1回目 (3m20s) | ||
| 時間計算量: O(N)<br> | ||
| 空間計算量: O(N) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left = 0 | ||
| max_length = 0 | ||
| letters_in_windows = set() | ||
|
|
||
| for right in range(len(s)): | ||
| while s[right] in letters_in_windows: | ||
| letters_in_windows.discard(s[left]) | ||
| left += 1 | ||
|
|
||
| letters_in_windows.add(s[right]) | ||
|
|
||
| max_length = max(max_length, right - left + 1) | ||
|
|
||
| return max_length | ||
| ``` | ||
|
|
||
| ## dictを使った解法 | ||
| --- | ||
| 時間計算量: O(N)<br> | ||
| 空間計算量: O(N) | ||
|
|
||
| (あまり旨みはない。indexが必要な時に便利か。) | ||
| ```python | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left = 0 | ||
| letter_to_index = defaultdict(int) | ||
| max_length = 0 | ||
|
|
||
| for right in range(len(s)): | ||
| while letter_to_index[s[right]] > 0: | ||
| letter_to_index[s[left]] -= 1 | ||
| left += 1 | ||
|
|
||
| letter_to_index[s[right]] += 1 | ||
| max_length = max(max_length, right - left + 1) | ||
|
|
||
| return max_length | ||
| ``` | ||
|
|
||
| ## dictを使った解法 (indexを記録) | ||
| --- | ||
| 時間計算量: O(N)<br> | ||
| 空間計算量: O(N) | ||
| ```python | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left = 0 | ||
| letter_to_index = defaultdict(int) | ||
|
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. これはdict()で良さそうです。 |
||
| max_length = 0 | ||
|
|
||
| for right in range(len(s)): | ||
| if s[right] in letter_to_index and left <= letter_to_index[s[right]]: | ||
| left = letter_to_index[s[right]] + 1 | ||
|
|
||
| letter_to_index[s[right]] = right | ||
| max_length = max(max_length, right - left + 1) | ||
|
|
||
| return max_length | ||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
dictを使うのであれば、value に index を指定してみてはどうでしょうか?今の実装だと value が 0 または 1 で実質 bool のように扱っているので、コメントにあるようにdictを使う旨味がなさそうです。(また 0 または 1 しか入らないにも関わらず int 型なのも少し違和感があります。){s[right]: right}として value に index を持たせる実装にすると、while 文を使わずに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.
すみません、ご指摘の通り、この実装はindexを記録できていないですね。
かなり意味ないことをしておりました。書き直します。
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.
アドバイスを基に書き直しました。