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 | ## その他 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 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 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..c41c476 --- /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_to_index = dict() + start = 0 + longest_length = 0 + for i, char in enumerate(s): + 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