Skip to content

Commit 2af285e

Browse files
committed
feat: add solutions to lc problem: No.0140
No.0140.Word Break II
1 parent 9b87b32 commit 2af285e

File tree

5 files changed

+475
-16
lines changed

5 files changed

+475
-16
lines changed

solution/0100-0199/0140.Word Break II/README.md

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,185 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:前缀树 + DFS**
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

5961
<!-- 这里可写当前语言的特殊实现逻辑 -->
6062

6163
```python
62-
64+
class Trie:
65+
def __init__(self):
66+
self.children = [None] * 26
67+
self.is_end = False
68+
69+
def insert(self, word):
70+
node = self
71+
for c in word:
72+
idx = ord(c) - ord('a')
73+
if node.children[idx] is None:
74+
node.children[idx] = Trie()
75+
node = node.children[idx]
76+
node.is_end = True
77+
78+
def search(self, word):
79+
node = self
80+
for c in word:
81+
idx = ord(c) - ord('a')
82+
if node.children[idx] is None:
83+
return False
84+
node = node.children[idx]
85+
return node.is_end
86+
87+
88+
class Solution:
89+
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
90+
def dfs(s):
91+
if not s:
92+
return [[]]
93+
res = []
94+
for i in range(1, len(s) + 1):
95+
if trie.search(s[:i]):
96+
for v in dfs(s[i:]):
97+
res.append([s[:i]] + v)
98+
return res
99+
100+
trie = Trie()
101+
for w in wordDict:
102+
trie.insert(w)
103+
ans = dfs(s)
104+
return [' '.join(v) for v in ans]
63105
```
64106

65107
### **Java**
66108

67109
<!-- 这里可写当前语言的特殊实现逻辑 -->
68110

69111
```java
112+
class Trie {
113+
Trie[] children = new Trie[26];
114+
boolean isEnd;
115+
116+
void insert(String word) {
117+
Trie node = this;
118+
for (char c : word.toCharArray()) {
119+
c -= 'a';
120+
if (node.children[c] == null) {
121+
node.children[c] = new Trie();
122+
}
123+
node = node.children[c];
124+
}
125+
node.isEnd = true;
126+
}
127+
128+
boolean search(String word) {
129+
Trie node = this;
130+
for (char c : word.toCharArray()) {
131+
c -= 'a';
132+
if (node.children[c] == null) {
133+
return false;
134+
}
135+
node = node.children[c];
136+
}
137+
return node.isEnd;
138+
}
139+
}
140+
141+
class Solution {
142+
private Trie trie = new Trie();
143+
144+
public List<String> wordBreak(String s, List<String> wordDict) {
145+
for (String w : wordDict) {
146+
trie.insert(w);
147+
}
148+
List<List<String>> res = dfs(s);
149+
return res.stream().map(e -> String.join(" ", e)).collect(Collectors.toList());
150+
}
151+
152+
private List<List<String>> dfs(String s) {
153+
List<List<String>> res = new ArrayList<>();
154+
if ("".equals(s)) {
155+
res.add(new ArrayList<>());
156+
return res;
157+
}
158+
for (int i = 1; i <= s.length(); ++i) {
159+
if (trie.search(s.substring(0, i))) {
160+
for (List<String> v : dfs(s.substring(i))) {
161+
v.add(0, s.substring(0, i));
162+
res.add(v);
163+
}
164+
}
165+
}
166+
return res;
167+
}
168+
}
169+
```
70170

171+
### **Go**
172+
173+
```go
174+
type Trie struct {
175+
children [26]*Trie
176+
isEnd bool
177+
}
178+
179+
func newTrie() *Trie {
180+
return &Trie{}
181+
}
182+
func (this *Trie) insert(word string) {
183+
node := this
184+
for _, c := range word {
185+
c -= 'a'
186+
if node.children[c] == nil {
187+
node.children[c] = newTrie()
188+
}
189+
node = node.children[c]
190+
}
191+
node.isEnd = true
192+
}
193+
func (this *Trie) search(word string) bool {
194+
node := this
195+
for _, c := range word {
196+
c -= 'a'
197+
if node.children[c] == nil {
198+
return false
199+
}
200+
node = node.children[c]
201+
}
202+
return node.isEnd
203+
}
204+
205+
func wordBreak(s string, wordDict []string) []string {
206+
trie := newTrie()
207+
for _, w := range wordDict {
208+
trie.insert(w)
209+
}
210+
var dfs func(string) [][]string
211+
dfs = func(s string) [][]string {
212+
res := [][]string{}
213+
if len(s) == 0 {
214+
res = append(res, []string{})
215+
return res
216+
}
217+
for i := 1; i <= len(s); i++ {
218+
if trie.search(s[:i]) {
219+
for _, v := range dfs(s[i:]) {
220+
v = append([]string{s[:i]}, v...)
221+
res = append(res, v)
222+
}
223+
}
224+
}
225+
return res
226+
}
227+
res := dfs(s)
228+
ans := []string{}
229+
for _, v := range res {
230+
ans = append(ans, strings.Join(v, " "))
231+
}
232+
return ans
233+
}
71234
```
72235

73236
### **...**

solution/0100-0199/0140.Word Break II/README_EN.md

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,174 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Trie:
53+
def __init__(self):
54+
self.children = [None] * 26
55+
self.is_end = False
56+
57+
def insert(self, word):
58+
node = self
59+
for c in word:
60+
idx = ord(c) - ord('a')
61+
if node.children[idx] is None:
62+
node.children[idx] = Trie()
63+
node = node.children[idx]
64+
node.is_end = True
65+
66+
def search(self, word):
67+
node = self
68+
for c in word:
69+
idx = ord(c) - ord('a')
70+
if node.children[idx] is None:
71+
return False
72+
node = node.children[idx]
73+
return node.is_end
74+
75+
76+
class Solution:
77+
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
78+
def dfs(s):
79+
if not s:
80+
return [[]]
81+
res = []
82+
for i in range(1, len(s) + 1):
83+
if trie.search(s[:i]):
84+
for v in dfs(s[i:]):
85+
res.append([s[:i]] + v)
86+
return res
87+
88+
trie = Trie()
89+
for w in wordDict:
90+
trie.insert(w)
91+
ans = dfs(s)
92+
return [' '.join(v) for v in ans]
5393
```
5494

5595
### **Java**
5696

5797
```java
98+
class Trie {
99+
Trie[] children = new Trie[26];
100+
boolean isEnd;
101+
102+
void insert(String word) {
103+
Trie node = this;
104+
for (char c : word.toCharArray()) {
105+
c -= 'a';
106+
if (node.children[c] == null) {
107+
node.children[c] = new Trie();
108+
}
109+
node = node.children[c];
110+
}
111+
node.isEnd = true;
112+
}
113+
114+
boolean search(String word) {
115+
Trie node = this;
116+
for (char c : word.toCharArray()) {
117+
c -= 'a';
118+
if (node.children[c] == null) {
119+
return false;
120+
}
121+
node = node.children[c];
122+
}
123+
return node.isEnd;
124+
}
125+
}
126+
127+
class Solution {
128+
private Trie trie = new Trie();
129+
130+
public List<String> wordBreak(String s, List<String> wordDict) {
131+
for (String w : wordDict) {
132+
trie.insert(w);
133+
}
134+
List<List<String>> res = dfs(s);
135+
return res.stream().map(e -> String.join(" ", e)).collect(Collectors.toList());
136+
}
137+
138+
private List<List<String>> dfs(String s) {
139+
List<List<String>> res = new ArrayList<>();
140+
if ("".equals(s)) {
141+
res.add(new ArrayList<>());
142+
return res;
143+
}
144+
for (int i = 1; i <= s.length(); ++i) {
145+
if (trie.search(s.substring(0, i))) {
146+
for (List<String> v : dfs(s.substring(i))) {
147+
v.add(0, s.substring(0, i));
148+
res.add(v);
149+
}
150+
}
151+
}
152+
return res;
153+
}
154+
}
155+
```
58156

157+
### **Go**
158+
159+
```go
160+
type Trie struct {
161+
children [26]*Trie
162+
isEnd bool
163+
}
164+
165+
func newTrie() *Trie {
166+
return &Trie{}
167+
}
168+
func (this *Trie) insert(word string) {
169+
node := this
170+
for _, c := range word {
171+
c -= 'a'
172+
if node.children[c] == nil {
173+
node.children[c] = newTrie()
174+
}
175+
node = node.children[c]
176+
}
177+
node.isEnd = true
178+
}
179+
func (this *Trie) search(word string) bool {
180+
node := this
181+
for _, c := range word {
182+
c -= 'a'
183+
if node.children[c] == nil {
184+
return false
185+
}
186+
node = node.children[c]
187+
}
188+
return node.isEnd
189+
}
190+
191+
func wordBreak(s string, wordDict []string) []string {
192+
trie := newTrie()
193+
for _, w := range wordDict {
194+
trie.insert(w)
195+
}
196+
var dfs func(string) [][]string
197+
dfs = func(s string) [][]string {
198+
res := [][]string{}
199+
if len(s) == 0 {
200+
res = append(res, []string{})
201+
return res
202+
}
203+
for i := 1; i <= len(s); i++ {
204+
if trie.search(s[:i]) {
205+
for _, v := range dfs(s[i:]) {
206+
v = append([]string{s[:i]}, v...)
207+
res = append(res, v)
208+
}
209+
}
210+
}
211+
return res
212+
}
213+
res := dfs(s)
214+
ans := []string{}
215+
for _, v := range res {
216+
ans = append(ans, strings.Join(v, " "))
217+
}
218+
return ans
219+
}
59220
```
60221

61222
### **...**

0 commit comments

Comments
 (0)