Skip to content

Create 139. Word Break.md#39

Open
fuga-98 wants to merge 1 commit into
mainfrom
139.-Word-Break
Open

Create 139. Word Break.md#39
fuga-98 wants to merge 1 commit into
mainfrom
139.-Word-Break

Conversation

@fuga-98

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

Copy link
Copy Markdown
Owner

@inatoihs inatoihs left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名の提案だけ書きました。

Comment thread 139. Word Break.md
def break_word_if_match(word):
for target in wordDict:
if word.startswith(target):
breaked_word = word[len(target):]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

breakedは不自然(正しくはbroken)なのと、この場合はrestとかでいい気がします。

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.

英語を忘れていました。

Comment thread 139. Word Break.md

stack = [s]
while stack:
word = stack[-1]

@inatoihs inatoihs May 1, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

wordと書くと何を指してるのかよく分からないので、currentとかでいいかもしれません。
他のものと合わせると、currentを関数に与えて得たsuffixが残るといった形になって僕にとっては読みやすくなる気がします。

Comment thread 139. Word Break.md
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
checked = set()

def break_word_if_match(word):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

break_word_if_matchだと何が戻ってくるのか分からないので、get_suffix_after_matchとかの方が良さそうです。

@fuga-98 fuga-98 May 1, 2025

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.

レビューありがとうございます。
分かりやすいですね。いまremove_prefix_if_matchも思いつきました。

Comment thread 139. Word Break.md
def break_word_if_match(word):
for target in wordDict:
if word.startswith(target):
breaked_word = word[len(target):]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、結果的に全部コピーしていますが、インデックスだけ保存するのも手です。
startswith はオプションの 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.

startswithを使えばコピーする必要がないんですね。気付かなかったです。

Comment thread 139. Word Break.md
if breaked_word == '':
return True
if breaked_word == word:
stack.pop()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

このコードはわりと直感的でないところがあると思います。
マッチさせるものは "aaaaab" に対して、["aaa", "ab", "aa"] としましょう。
このコードは breaked_word("aaaaab") が2回呼ばれてしかも結果が変わりますね。"aab" と "aaab" です。
これ、結構驚きじゃないですか。
私だったら全候補を返します。

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.

確かに直感的ではないですね。
このソースはDFSっぽいイメージをもって書いたので一つ返しました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

参照透過性がないということですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

二分木のdfsも再帰を使うと思っている通りのdfsになりますが、スタックを使うとそうならないので、多少ジグザグする探索でもdfsとして受け入れられると思います。

Comment thread 139. Word Break.md
Comment on lines +89 to +91
- Acceptされることを喜ばないほうが良い
- 書く能力よりも読む能力が足りていない。
- 脳内のデバッガーを走らせる

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • Acceptされることを喜ばないほうが良い
  • 書く能力よりも読む能力が足りていない。
  • 脳内のデバッガーを走らせる

いいこと言ってますね。

Comment thread 139. Word Break.md

def break_word_if_match(word):
for target in wordDict:
if word.startswith(target):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

word: 文字列sの任意のi以降
target: wordListの単語の一つ

こう整理するとwordという変数名の対応関係が変になっていると思います。つまり、wordという変数名があったらwordListの単語の一つかなと思いたくなりますし、sのi以降の文字列に対して「単語」という意味のwordという変数名をつけるのも変だと思いました。

@fuga-98 fuga-98 May 2, 2025

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.

文字列を変数名にするのが上手くできなくてそう書いてます。word→string_、target→wordとかが分かりやすいでしょうか。

@hroc135 hroc135 May 2, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そうですね、私はword -> s_suffixみたいにするかなと思いました。

@hroc135

hroc135 commented May 2, 2025

Copy link
Copy Markdown

動的計画法ぽくやる方法もあります!
break後に先頭になり得るインデックスをTrueとした配列を用意する(長さはlen(s))
インデックス0はTrue
前から舐めてTrueのインデックスで止まり、wordListの中から当てはめられるwordを探す。見つかったらbreak後に先頭になるインデックスをTrueに変える

みたいな感じです。
こうすると空間計算量をO(len(s))で抑えられます。

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