From 67c43880adb22635f47b6c818fcdfafe5b799e41 Mon Sep 17 00:00:00 2001 From: Tomoya Honda Date: Fri, 26 Apr 2024 15:59:07 +0000 Subject: [PATCH 1/5] Add step1 --- ...ring-without-repeating-characters.step1.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 problems/medium/python3/3/longest-substring-without-repeating-characters.step1.py diff --git a/problems/medium/python3/3/longest-substring-without-repeating-characters.step1.py b/problems/medium/python3/3/longest-substring-without-repeating-characters.step1.py new file mode 100644 index 0000000..df1e014 --- /dev/null +++ b/problems/medium/python3/3/longest-substring-without-repeating-characters.step1.py @@ -0,0 +1,19 @@ +# +# @lc app=leetcode id=3 lang=python3 +# +# [3] Longest Substring Without Repeating Characters +# + +# @lc code=start +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + index_map = dict() + start = -1 + length = 0 + for i, char in enumerate(s): + if char in index_map: + start = max(start, index_map[char]) + index_map[char] = i + length = max(length, i - start) + return length +# @lc code=end From e7899380c21192cc4d365030902e3521e7ac1658 Mon Sep 17 00:00:00 2001 From: Tomoya Honda Date: Fri, 26 Apr 2024 16:09:02 +0000 Subject: [PATCH 2/5] Add step2 --- ...ring-without-repeating-characters.step2.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 problems/medium/python3/3/longest-substring-without-repeating-characters.step2.py diff --git a/problems/medium/python3/3/longest-substring-without-repeating-characters.step2.py b/problems/medium/python3/3/longest-substring-without-repeating-characters.step2.py new file mode 100644 index 0000000..89c3c23 --- /dev/null +++ b/problems/medium/python3/3/longest-substring-without-repeating-characters.step2.py @@ -0,0 +1,19 @@ +# +# @lc app=leetcode id=3 lang=python3 +# +# [3] Longest Substring Without Repeating Characters +# + +# @lc code=start +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + char_index_map = dict() + start = -1 + longest_length = 0 + for i, char in enumerate(s): + if char in char_index_map: + start = max(start, char_index_map[char]) + char_index_map[char] = i + longest_length = max(longest_length, i - start) + return longest_length +# @lc code=end From 7c7303dce3610da6fa380cc7b4e55fe439655e15 Mon Sep 17 00:00:00 2001 From: Tomoya Honda Date: Sat, 27 Apr 2024 01:28:17 +0000 Subject: [PATCH 3/5] Add step3 --- ...ring-without-repeating-characters.step3.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py diff --git a/problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py b/problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py new file mode 100644 index 0000000..067d4f4 --- /dev/null +++ b/problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py @@ -0,0 +1,19 @@ +# +# @lc app=leetcode id=3 lang=python3 +# +# [3] Longest Substring Without Repeating Characters +# + +# @lc code=start +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + char_index_map = dict() + start = 0 + longest_length = 0 + for i, char in enumerate(s): + if char in char_index_map: + start = max(start, char_index_map[char] + 1) + char_index_map[char] = i + longest_length = max(longest_length, i - start + 1) + return longest_length +# @lc code=end From 935dd2cb9fc34d0cd367731cf5bf987b97fb09b0 Mon Sep 17 00:00:00 2001 From: Tomoya Honda Date: Sat, 27 Apr 2024 03:05:27 +0000 Subject: [PATCH 4/5] Rename variable name --- ...ongest-substring-without-repeating-characters.step3.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py b/problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py index 067d4f4..c41c476 100644 --- a/problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py +++ b/problems/medium/python3/3/longest-substring-without-repeating-characters.step3.py @@ -7,13 +7,13 @@ # @lc code=start class Solution: def lengthOfLongestSubstring(self, s: str) -> int: - char_index_map = dict() + char_to_index = dict() start = 0 longest_length = 0 for i, char in enumerate(s): - if char in char_index_map: - start = max(start, char_index_map[char] + 1) - char_index_map[char] = i + if char in char_to_index: + start = max(start, char_to_index[char] + 1) + char_to_index[char] = i longest_length = max(longest_length, i - start + 1) return longest_length # @lc code=end From c04fd480c6ef8654e8017fceadf7cf0dd738f6a8 Mon Sep 17 00:00:00 2001 From: Tomoya Honda Date: Sat, 27 Apr 2024 03:06:35 +0000 Subject: [PATCH 5/5] Add title to README --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b8215fd..6693844 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,14 @@ LeetCode の問題を以下手順で解く ## 解いた問題一覧 -| # | Title | Language | Difficulty | -| --- | --------------------------------------------------------------------------------------------------------- | -------- | ---------- | -| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/description/) | Python3 | Medium | -| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/description/) | Python3 | Medium | -| 207 | [Course Schedule](https://leetcode.com/problems/ourse-schedule/description/) | Python3 | Medium | -| 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | Python3 | Medium | -| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | Python3 | Medium | +| # | Title | Language | Difficulty | +| --- | ------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ---------- | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/) | Python3 | Medium | +| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/description/) | Python3 | Medium | +| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/description/) | Python3 | Medium | +| 207 | [Course Schedule](https://leetcode.com/problems/ourse-schedule/description/) | Python3 | Medium | +| 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | Python3 | Medium | +| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | Python3 | Medium | ## その他