-
Notifications
You must be signed in to change notification settings - Fork 0
3. Longest Substring Without Repeating Characters #42
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
akmhmgc
wants to merge
1
commit into
main
Choose a base branch
from
3
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,29 @@ | ||
| # step1 何も見ずに解く | ||
| 文字を前から一つずつみていって、開始時点から今見てるところまでの文字列がすべてユニークな文字を含む文字列かどうかをチェックしたい。 | ||
| 見た文字を管理すれば今見ている文字が既に見たことあるかどうかがわかる。 | ||
| 既に見た文字があるとき、この文字を含む場合どこまで後ろであれば問題ないかを知りたい。 | ||
| 例えばabcdec....という文字列がある時に、二回目のcが来た時にdecは問題ないので次を見に行く、ということをしたい。 | ||
| それを実現するには、今見ている文字がなくなるまで開始時点を進めればよい。 | ||
| こういう感じの発想でいけるはず。 | ||
|
|
||
| 時間計算量はO(N)で空間計算量もO(N) | ||
| Nの最大値は5 * 10^4なので1秒以内に終わる。 | ||
|
|
||
| ```ruby | ||
| # @param {String} str | ||
| # @return {Integer} | ||
| def length_of_longest_substring(str) | ||
| chars_in_substr = Set.new | ||
| l = 0 | ||
| res = 0 | ||
| str.size.times do |r| | ||
| while chars_in_substr.include?(str[r]) | ||
| chars_in_substr.delete(str[l]) | ||
| l += 1 | ||
| end | ||
| chars_in_substr << str[r] | ||
| res = [res, r - l + 1].max | ||
| end | ||
| res | ||
| end | ||
| ``` |
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,21 @@ | ||
| # step2 他の方の解答を見る | ||
| https://github.com/olsen-blue/Arai60/pull/49 | ||
| ハッシュテーブルを使う方法 | ||
|
|
||
| ```ruby | ||
| # @param {String} str | ||
| # @return {Integer} | ||
| def length_of_longest_substring(str) | ||
| visited_char_to_index = Hash.new(-1) | ||
| max_length = 0 | ||
| left = 0 | ||
| str.size.times do |right| | ||
| left = [left, visited_char_to_index[str[right]] + 1].max | ||
| max_length = [max_length, right - left + 1].max | ||
| visited_char_to_index[str[right]] = right | ||
| end | ||
| max_length | ||
| end | ||
| ``` | ||
|
|
||
| こっちの方がシンプルでわかりやすいかも |
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,17 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| ```ruby | ||
| # @param {String} str | ||
| # @return {Integer} | ||
| def length_of_longest_substring(str) | ||
| visited_char_to_index = Hash.new(-1) | ||
| max_size = 0 | ||
| left = 0 | ||
| str.size.times do |right| | ||
| left = [left, visited_char_to_index[str[right]] + 1].max | ||
| max_size = [max_size, right - left + 1].max | ||
| visited_char_to_index[str[right]] = right | ||
| end | ||
| max_size | ||
| end | ||
| ``` | ||
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 @@ | ||
| ## step4 レビューを受けて解答を修正 |
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.
良いと思いました。visited_char_to_index は難しいところですが、どういうインデックスかを説明する
char_to_last_index や char_to_last_seen_index もありかもしれません。やや長いので微妙でしょうか。