-
Notifications
You must be signed in to change notification settings - Fork 0
139. Word Break #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
139. Word Break #41
Conversation
| ```python | ||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| candidates = [w for w in wordDict] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
これ知りませんでした。
| if candidate in found: | ||
| continue | ||
| found.add(candidate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このチェック機構、candidates に入るところでチェックするのと出てきたところでチェックするのがあって、この場合は、出てきたところのチェックでも DFS なので大丈夫ですね。
| word_min_len = min([len(word) for word in words]) | ||
| word_max_len = max([len(word) for word in words]) |
There was a problem hiding this comment.
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) でどうでしょうか。
There was a problem hiding this comment.
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 = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ中身、words(複数形)と言えますかね。ちょっと微妙なような。
単にprefixとかでもいい気がします。(もしくはprefix_characters?)
There was a problem hiding this comment.
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):]): |
There was a problem hiding this comment.
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))})*$" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1行でやってることがやや多いので、escapeのあたりで一旦変数置いて分けたほうがわかりやすそうですね。
また、ユーザーからの入力は本当は正規表現に入れるとやばいですね。
There was a problem hiding this comment.
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
philip82148
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いいと思います!
https://leetcode.com/problems/word-break/description/