Skip to content

Conversation

@Kaichi-Irie
Copy link
Owner

問題へのリンク

3. Longest Substring Without Repeating Characters

言語

Python

問題の概要

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

自分の解法

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

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

  • 時間計算量:O(n)

  • 空間計算量:O(1)

step2

  • 変数名を変更:substring_start_indexwindow_start_index
  • コメントを追加

別解・模範解答

ウィンドウ制御にsetを用いる方法もある。

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

while char in window_chars:
    window_chars.remove(s[left_index])  # O(1)
    left_index += 1
  • 時間計算量:O(n)
  • 空間計算量:O(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だけで済むような場合もありそうですね、参考にさせていただきます。

# 別解・模範解答
ウィンドウ制御に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.

ありがとうございます

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 である必要は無いですね、ご指摘ありがとうございます。

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