Skip to content

3. Longest Substring Without Repeating Characters#6

Merged
thonda28 merged 5 commits intomainfrom
3.longest-substring-without-repeating-characters
Apr 27, 2024
Merged

3. Longest Substring Without Repeating Characters#6
thonda28 merged 5 commits intomainfrom
3.longest-substring-without-repeating-characters

Conversation

@thonda28
Copy link
Copy Markdown
Owner

Problem

Description

  • Step1
    • 12分で Pass
    • 時間計算量:O(N)、空間計算量:O(N)
    • 過去に出た数字が出てきたら始点を変更する(方針はすぐに思いついたが、以下のミスに気づくのに 7-8 分を使った)
      • start = max(start, index_map[char]) の部分を start = index_map[char] としていた
      • start = 0 と初期値を誤っていた(s = "a" のような1文字の場合に明らかに間違いだと気づける)
  • Step2
    • 以下の命名を修正
      • index_map -> char_index_map
      • length -> longest_length
  • Step3
    • Step1 で15分以内に解けたので Skip

Note

  • 同じ問題を6ヶ月前に解いた経験あり

Tomoya Honda added 2 commits April 26, 2024 15:59
# @lc code=start
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
char_index_map = dict()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

char_index_map = {}
名前は、char_to_indexなどでもいいでしょう。

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.

コメントありがとうございます。char_to_index のほうがわかりやすそうですね、修正します。

初期化の方法ですが、{} はパッと見で dictset かわかりにくいので、中身が空のときには明示的に dict()set() と書くように意識していました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こんなコメントを頂いた事があります。
TORUS0818/leetcode#13 (comment)

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
char_index_map = dict()
start = -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がexclusive(開区間)なのが、途中まで気づきませんでした。コメントなどで追記するか、inclusiveにすると分かりやすいかと思います。

Copy link
Copy Markdown
Owner Author

@thonda28 thonda28 Apr 27, 2024

Choose a reason for hiding this comment

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

コメントありがとうございます。同じ文字が出てきた場合、過去に出てきたときの index が exclusive になるので合わせて start も exclusive にしていましたが、初見でコードを読む人に(コメントもなしで)それを汲み取らせるのはたしかに酷に思いました。start という変数名にそれ自身が含まれていないことにも違和感がありそう(読み手は inclusive に感じそう)です。

7c7303d で inclusive 版も提出してみました。

start = 0
longest_length = 0
for i, char in enumerate(s):
if char in char_index_map:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

max を if の中の and にするのも一つです。

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.

コメントありがとうございます。startchar_index_map[char] + 1 の比較も if 文の and 条件に入れてしまうということですね。この問題を書いているときは max 一択でその他の選択肢をまったく考えていなかったので、他の選択肢もあるか考えるようにしてみます。

Copy link
Copy Markdown

@liquo-rice liquo-rice left a comment

Choose a reason for hiding this comment

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

良さそうです。

@thonda28
Copy link
Copy Markdown
Owner Author

良さそうです。

Approve ありがとうございます。char_to_index の変数名を変えるのを忘れていたので、935dd2c で修正しました。この PR はこれでマージします。

@thonda28 thonda28 merged commit f722f11 into main Apr 27, 2024
@thonda28 thonda28 deleted the 3.longest-substring-without-repeating-characters branch April 27, 2024 03:08
start = 0
longest_length = 0
for i, char in enumerate(s):
if char in char_to_index:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

max を and にする手はある気がしますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(すみません、同じコメントをしてしまいました。)

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.

4 participants