Skip to content

Create 139. Word Break.md#39

Open
tokuhirat wants to merge 1 commit intomainfrom
139.-Word-Break
Open

Create 139. Word Break.md#39
tokuhirat wants to merge 1 commit intomainfrom
139.-Word-Break

Conversation

@tokuhirat
Copy link
Owner

This Problem
139. Word Break
Next Ploblem
322. Coin Change
言語: Python

@Mike0121
Copy link

自分は良いと思いました。StartWith使うとコードがスッキリしますね。
問題文には、All the strings of wordDict are unique.とありますが、uniqueでないものが入ってる場合のテストケースを考えても面白いかもとも思いました。

if s.startswith(word, start):
is_breakable[start + len(word)] = True
return is_breakable[-1]
```

Choose a reason for hiding this comment

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

読みやすかったです。いいと思います。


wordBreak 内で s と Trie木 をマッチングする書き方
```python
class TrieNode:
Copy link

Choose a reason for hiding this comment

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

単独のnodeの部分と、trieの木の部分とでクラスを分ける方が自分の好みです。

return is_breakable[-1]
```

TrieNode.get_match_lengths を実装したパターン。こちらの方が機能が分離されていて良いと思った。
Copy link

Choose a reason for hiding this comment

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

自分もそう思いました

```python
class TrieNode:
def __init__(self):
self.next = {}
Copy link

Choose a reason for hiding this comment

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

https://docs.python.org/ja/3/library/functions.html#next
nextが組み込み関数の名前と衝突してるので、ややこしいことになるかもしれません

node = node.next[char]
node.is_word_end = True

def get_match_lengths(self, s):
Copy link

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