Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 3_longest_substring_without_repeating_characters_medium/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 問題へのリンク

[3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)

# 言語
Python

# 問題の概要
文字列 `s` が与えられたとき、重複しない文字からなる最長の部分文字列(substring)の長さを求める問題。

# 自分の解法

文字列から文字`char`を1文字ずつ取り出していく。文字`char`を走査しているとき、そこから前にたどって最長の部分文字列(で文字が重複していないもの)を管理しながら走査していく。

- 各文字に対して、それが出現した最後のインデックスを記録するための辞書 `char_to_last_index` を用意してウィンドウ制御を行う。


- 時間計算量:`O(n)`
- 空間計算量:`O(1)`

## step2
- 変数名を変更:`substring_start_index` → `window_start_index`
- コメントを追加


# 別解・模範解答
ウィンドウ制御にsetを用いる方法もある。

https://wiki.python.org/moin/TimeComplexity より、setの要素の追加・削除は平均して`O(1)`であるため、以下のように書くことができる。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setの実装を見ても良いかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ぜひみてみます。
もしおすすめの資料などあれば教えていただきたいです。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここらへんが実装だと思います。
https://github.com/python/cpython/blob/main/Objects/setobject.c

AIにcpythonのsetの実装箇所を聞くと答えてくれたりします。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます


```python
while char in window_chars:
window_chars.remove(s[left_index]) # O(1)
left_index += 1
```

- Sliding Windowでは、基本的に`[left, right]`の閉区間で、`right`をforループでインクリメントしていくのが王道。バグも生みにくい。


- 時間計算量:`O(n)`
- 空間計算量:`O(1)`

# 次に解く問題の予告
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# @lc app=leetcode id=3 lang=python3
#
# [3] Longest Substring Without Repeating Characters
#


# @lc code=start
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
window_chars: set[str] = set()
max_length = 0
left_index = 0
for right_index, char in enumerate(s):
while char in window_chars:
window_chars.remove(s[left_index]) # O(1) on average
left_index += 1
window_chars.add(char)
max_length = max(max_length, right_index - left_index + 1)

return max_length


# @lc code=end
32 changes: 32 additions & 0 deletions 3_longest_substring_without_repeating_characters_medium/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# @lc app=leetcode id=3 lang=python3
#
# [3] Longest Substring Without Repeating Characters
#


# @lc code=start
from collections import defaultdict


class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:
return 0

# character -> last index
char_to_last_index: dict[str, int] = defaultdict(int)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、defaultdictである必要はありますか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにこのコードでは if char in char_to_last_index: が入っているので defaultdict である必要は無いですね、ご指摘ありがとうございます。

substring_start_index = 0
max_length = 0

for index, char in enumerate(s):
if char in char_to_last_index:
substring_start_index = max(
substring_start_index, char_to_last_index[char] + 1
)
char_to_last_index[char] = index
max_length = max(max_length, index - substring_start_index + 1)
return max_length


# @lc code=end
36 changes: 36 additions & 0 deletions 3_longest_substring_without_repeating_characters_medium/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# @lc app=leetcode id=3 lang=python3
#
# [3] Longest Substring Without Repeating Characters
#


# @lc code=start
from collections import defaultdict


class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:
return 0

# character -> last index
char_to_last_index: dict[str, int] = defaultdict(int)
window_start_index = 0
max_length = 0

# consider two substrings at every iteration;
# 1. trailing substring: starts at substring_start_index, ends at index, and managed by a window
# 2. last longest substring: only length information is saved as max_length
# at the end of every iteration, we update max_length
for index, char in enumerate(s):
if char in char_to_last_index:
window_start_index = max(
window_start_index, char_to_last_index[char] + 1
)
char_to_last_index[char] = index
max_length = max(max_length, index - window_start_index + 1)
return max_length


# @lc code=end
29 changes: 29 additions & 0 deletions 3_longest_substring_without_repeating_characters_medium/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# @lc app=leetcode id=3 lang=python3
#
# [3] Longest Substring Without Repeating Characters
#

# @lc code=start
from collections import defaultdict


class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# character -> last index
char_to_last_index: dict[str, int] = defaultdict(int)

# window manages the trailing substring
window_start_index = 0
max_length = 0
for index, char in enumerate(s):
if char in char_to_last_index:
window_start_index = max(
window_start_index, char_to_last_index[char] + 1
)
max_length = max(max_length, index - window_start_index + 1)
char_to_last_index[char] = index
return max_length

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getを使うとこのように書けます。

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        start = 0
        char_to_last_index = {}
        max_length = 0
        for i, c in enumerate(s):
            start = max(start, char_to_last_index.get(c, -1) + 1)
            max_length = max(max_length, i - start + 1)
            char_to_last_index[c] = i
        return max_length

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューありがとうございます。
defaultdictを使わずにgetだけで済むような場合もありそうですね、参考にさせていただきます。


# @lc code=end