Skip to content

Conversation

@TORUS0818
Copy link
Owner

```python
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
candidates = [w for w in wordDict]
Copy link

Choose a reason for hiding this comment

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

これ、wordDict[:] や wordDict.copy() や list(wordDict) や copy.copy(wordDict) などあるでしょう。
https://stackoverflow.com/questions/2612802/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment

Copy link
Owner Author

Choose a reason for hiding this comment

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

new_list = list(old_list)
これ知りませんでした。

Comment on lines +22 to +24
if candidate in found:
continue
found.add(candidate)
Copy link

Choose a reason for hiding this comment

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

このチェック機構、candidates に入るところでチェックするのと出てきたところでチェックするのがあって、この場合は、出てきたところのチェックでも DFS なので大丈夫ですね。

Comment on lines +162 to +163
word_min_len = min([len(word) for word in words])
word_max_len = max([len(word) for word in words])
Copy link

Choose a reason for hiding this comment

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

これするならば、set(len(word) for word in words) でどうでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

こちら、頭にありませんでした。
端だけでなくstepも最適化できましたね。。


def find_prefix_words(self, s: str) -> Iterator[str]:
node = self.root
prefix_words = []
Copy link

Choose a reason for hiding this comment

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

これ中身、words(複数形)と言えますかね。ちょっと微妙なような。
単にprefixとかでもいい気がします。(もしくはprefix_characters?)

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。確かにwordじゃなかったですね。。

if not s:
return True
for word in trie.find_prefix_words(s):
if word_break_helper(s[len(word):]):
Copy link

Choose a reason for hiding this comment

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

スライスでコピーが発生するのはちょっと気になりますね

```python
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
pattern = f"^({'|'.join(map(re.escape, wordDict))})*$"
Copy link

Choose a reason for hiding this comment

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

1行でやってることがやや多いので、escapeのあたりで一旦変数置いて分けたほうがわかりやすそうですね。
また、ユーザーからの入力は本当は正規表現に入れるとやばいですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

元々正規表現自体が分かりにくいので、escape処理くらいは混ぜちゃってもいいかなという気持ちでした。

想起するのはReDosとかでしょうか。
https://en.wikipedia.org/wiki/ReDoS

Copy link

@philip82148 philip82148 left a comment

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.

5 participants