From 4d1d9e43f482d2544e6a6f18ae0b644d5315dfd6 Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Sat, 27 Apr 2024 10:56:29 +0900 Subject: [PATCH 1/3] complete --- .../step1.py | 24 +++++++++++++++++++ .../step2.py | 19 +++++++++++++++ .../step3.py | 17 +++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 arai60/longest-substring-without-repeating-characters/step1.py create mode 100644 arai60/longest-substring-without-repeating-characters/step2.py create mode 100644 arai60/longest-substring-without-repeating-characters/step3.py diff --git a/arai60/longest-substring-without-repeating-characters/step1.py b/arai60/longest-substring-without-repeating-characters/step1.py new file mode 100644 index 0000000..9f5d447 --- /dev/null +++ b/arai60/longest-substring-without-repeating-characters/step1.py @@ -0,0 +1,24 @@ +""" +出現した文字を集合で管理して判定するとよさそう +連続でなければならないのに気づいていなかった。サンプルを最初にちゃんと確認しなければ。 +連続ならば、すでに出現したものが現れたときにリセットすればよい +いや、被りが来た時点で、被りの次のindexから始めることにしないといけない +辞書で管理するほうが良いか +if seen.get(s[i]) andというような条件にしてしまっていて、seen.get(s[i]) == 0の場合が弾かれてしまっていたのに気づくのに時間がかかってしまった... + +経過時間:22m +""" + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + seen = dict() + answer = 0 + start_index = 0 + for i in range(len(s)): + if s[i] in seen and seen.get(s[i]) >= start_index: + answer = max(answer, i-start_index) + start_index = seen.get(s[i])+1 + seen[s[i]] = i + answer = max(answer, len(s)-start_index) + + return answer diff --git a/arai60/longest-substring-without-repeating-characters/step2.py b/arai60/longest-substring-without-repeating-characters/step2.py new file mode 100644 index 0000000..0db38f0 --- /dev/null +++ b/arai60/longest-substring-without-repeating-characters/step2.py @@ -0,0 +1,19 @@ +""" +算術をスペース入れずにやってしまう癖を直す +start_index → left +enumerateを使う +answerに代入する処理の位置を変える +""" + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + seen = dict() + answer = 0 + left = 0 + for right, c in enumerate(s): + if c in seen and seen[c] >= left: + left = seen[c] + 1 + seen[c] = right + answer = max(answer, right - left + 1) + + return answer diff --git a/arai60/longest-substring-without-repeating-characters/step3.py b/arai60/longest-substring-without-repeating-characters/step3.py new file mode 100644 index 0000000..bf2384c --- /dev/null +++ b/arai60/longest-substring-without-repeating-characters/step3.py @@ -0,0 +1,17 @@ +""" +1回目: 3m55s + +""" + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + seen = dict() + answer = 0 + left = 0 + for right, c in enumerate(s): + if c in seen and seen[c] >= left: + left = seen[c] + 1 + seen[c] = right + answer = max(answer, right - left + 1) + + return answer From e7c4c3ca118bb4131089c8b9329fe7bdb124dab2 Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Sat, 27 Apr 2024 11:07:57 +0900 Subject: [PATCH 2/3] change seen to char_to_index --- .../step2.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arai60/longest-substring-without-repeating-characters/step2.py b/arai60/longest-substring-without-repeating-characters/step2.py index 0db38f0..3215427 100644 --- a/arai60/longest-substring-without-repeating-characters/step2.py +++ b/arai60/longest-substring-without-repeating-characters/step2.py @@ -3,17 +3,19 @@ start_index → left enumerateを使う answerに代入する処理の位置を変える + +char_to_indexのほうがよさそう """ class Solution: def lengthOfLongestSubstring(self, s: str) -> int: - seen = dict() + char_to_index = dict() answer = 0 left = 0 for right, c in enumerate(s): - if c in seen and seen[c] >= left: - left = seen[c] + 1 - seen[c] = right + if c in char_to_index and char_to_index[c] >= left: + left = char_to_index[c] + 1 + char_to_index[c] = right answer = max(answer, right - left + 1) return answer From e75dc68271cc4a62f9961eba4930aec2fb0b5fd9 Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Sat, 27 Apr 2024 13:20:57 +0900 Subject: [PATCH 3/3] add step4 --- .../step4.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 arai60/longest-substring-without-repeating-characters/step4.py diff --git a/arai60/longest-substring-without-repeating-characters/step4.py b/arai60/longest-substring-without-repeating-characters/step4.py new file mode 100644 index 0000000..384a9ec --- /dev/null +++ b/arai60/longest-substring-without-repeating-characters/step4.py @@ -0,0 +1,16 @@ +""" +returnの前の改行をなくす +answer → max_length +""" + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + char_to_index = dict() + max_length = 0 + left = 0 + for right, c in enumerate(s): + if c in char_to_index and char_to_index[c] >= left: + left = char_to_index[c] + 1 + char_to_index[c] = right + max_length = max(max_length, right - left + 1) + return max_length