Skip to content

Conversation

@skypenguins
Copy link
Owner

3. Longest Substring Without Repeating Characters

@skypenguins skypenguins self-assigned this May 24, 2025
@skypenguins skypenguins changed the title # 3. Longest Substring Without Repeating Characters 3. Longest Substring Without Repeating Characters May 24, 2025
* 実行時間: TLE

* いつまで経っても `tail` == `len(s)-1` とならないため、TLEとなった
- 上記原因が分からず、1時間経っていたため答えを見た
Copy link

Choose a reason for hiding this comment

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

tail -= 1 してますからね。
本当はしたかったことは、
substring = substring[1:]
だとは思うのですが、しかし、それだと s[tail] がまだ中にあるかもしれません。なので頭から探して、そこまで動かさないといけません。

頭の中のシミュレーターの精度を上げましょう。それと、「人が手でやるとしたらどうするか」と「複数人でシフトを組んでやってもらうとしたらどうするか」を考えてみるといいかと思います。

def lengthOfLongestSubstring(self, s: str) -> int:
left_i = 0
max_substring_length = 0
seen_char_set = set()
Copy link

Choose a reason for hiding this comment

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

個人的にはseenだと一度見たものは取り除かれないみたいな風に見えてしまうかもしれないので、containing_char_setとかでもいいと思います。

substring_length = 0
substring = ''
tail = 0
while tail != len(s)-1:
Copy link

Choose a reason for hiding this comment

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

- の両側にスペースを空けることをお勧めします。

def lengthOfLongestSubstring(self, s: str) -> int:
left_i = 0
max_substring_length = 0
char_set = set()
Copy link

Choose a reason for hiding this comment

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

変数名に型名を入れてもあまり情報は増えないため、入れないほうがよいと思います。

max_substring_length = 0
seen_char_set = set()

for right_i in range(len(s)):
Copy link

Choose a reason for hiding this comment

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

(コメントが保留中のまま送れませんでした、失礼しました…)
memoが整理されていて読みやすかったです!
好みの範疇だと思うのですが、right_i自体が必要な個所が少ないので、
for right_i, right_char in enumerate(s):
でもいいかなと思いました。

```python
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
left_i = 0

Choose a reason for hiding this comment

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

以前以下のようなコメントをいただいた事があります。
TORUS0818/leetcode#35 (comment)

tail = 0
while tail != len(s)-1:
substring_length = max(substring_length, len(substring))
substring += s[tail]

Choose a reason for hiding this comment

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

こういう話題もあったので共有します。
nittoco/leetcode#4 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants