Skip to content

Commit 93310dd

Browse files
solves word break in java
1 parent c09ffe3 commit 93310dd

File tree

2 files changed

+18
-50
lines changed

2 files changed

+18
-50
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
| 136 | [Single Number](https://leetcode.com/problems/single-number) | [![Java](assets/java.png)](src/SingleNumber.java) [![Python](assets/python.png)](python/single_number.py) | |
125125
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii) | [![Java](assets/java.png)](src/SingleNumberII.java) | |
126126
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer) | [![Java](assets/java.png)](src/CopyListWithRandomPointer.java) | |
127-
| 139 | [Word Break](https://leetcode.com/problems/word-break) | | |
127+
| 139 | [Word Break](https://leetcode.com/problems/word-break) | [![Java](assets/java.png)](src/WordBreak.java) | |
128128
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [![Java](assets/java.png)](src/LinkedListCycle.java) [![Python](assets/python.png)](python/linked_list_cycle.py) | |
129129
| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii) | | |
130130
| 143 | [Reorder List](https://leetcode.com/problems/reorder-list) | | |

src/WordBreak.java

+17-49
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,26 @@
1+
// https://leetcode.com/problems/word-break
2+
// T: O(s^3 + w)
3+
// S: O(s + w)
4+
5+
import java.util.HashSet;
16
import java.util.List;
7+
import java.util.Set;
28

39
public class WordBreak {
4-
private static final class Trie {
5-
private static final int LETTERS_IN_ENGLISH_ALPHABET = 26;
6-
7-
Trie[] letters = new Trie[LETTERS_IN_ENGLISH_ALPHABET];
8-
boolean isWord = false;
9-
10-
public void insert(String word) {
11-
insert(word, 0, this);
12-
}
13-
14-
private void insert(String s, int index, Trie root) {
15-
if (index >= s.length()) {
16-
root.isWord = true;
17-
return;
18-
}
19-
if (root.letters[s.charAt(index) - 'a'] == null) {
20-
root.letters[s.charAt(index) - 'a'] = new Trie();
21-
}
22-
insert(s, index + 1, root.letters[s.charAt(index) - 'a']);
23-
}
24-
25-
public boolean exists(String word) {
26-
return exists(word, 0, this);
27-
}
28-
29-
private boolean exists(String s, int index, Trie root) {
30-
if (index >= s.length()) {
31-
return root.isWord;
32-
}
33-
if (root.letters[s.charAt(index) - 'a'] == null) return false;
34-
return exists(s, index + 1, root.letters[s.charAt(index) - 'a']);
35-
}
36-
}
37-
38-
3910
public boolean wordBreak(String s, List<String> wordDict) {
40-
final Trie trie = new Trie();
41-
for (String string : wordDict) {
42-
trie.insert(string);
43-
}
44-
return wordBreak(s, trie);
45-
}
46-
47-
private boolean wordBreak(String s, Trie root) {
48-
if (s.isEmpty()) return true;
49-
50-
for (int i = 0 ; i < s.length() ; i++) {
51-
if (root.exists(s.substring(0, i + 1)) && wordBreak(s.substring(i + 1), root)) {
52-
return true;
11+
final Set<String> words = new HashSet<>(wordDict);
12+
final boolean[] dp = new boolean[s.length() + 1];
13+
dp[0] = true;
14+
15+
for (int i = 1 ; i <= s.length() ; i++) {
16+
for (int j = 0 ; j < i ; j++) {
17+
if (dp[j] && words.contains(s.substring(j, i))) {
18+
dp[i] = true;
19+
break;
20+
}
5321
}
5422
}
5523

56-
return false;
24+
return dp[dp.length - 1];
5725
}
5826
}

0 commit comments

Comments
 (0)