Skip to content

Create Longest Substring Without Repeating Characters.md#47

Open
fuga-98 wants to merge 1 commit into
mainfrom
Longest-Substring-Without-Repeating-Characters
Open

Create Longest Substring Without Repeating Characters.md#47
fuga-98 wants to merge 1 commit into
mainfrom
Longest-Substring-Without-Repeating-Characters

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 17, 2025

return max_length
```

+1が複数あるのでイメージしながら書かないとミスりますね
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一つは、start が閉区間になっているというのもあるので、start - 1 を新しい start と置くと、すっきりするかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

開区間で書いたら見やすくなりました。

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


実行時間は5/10**4 / 10**6 = 5msくらい

そういえばLeetcodeのLTEってどれくらいなんだろう
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: TLE(time limit exceeded)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants