-
Notifications
You must be signed in to change notification settings - Fork 91
/
Word_Break_II.java
84 lines (70 loc) · 2.73 KB
/
Word_Break_II.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Leetcodelink - https://leetcode.com/problems/word-break-ii/
class Solution {
int[] exists;
Trie root;
public void search(List<String> ans, Trie curr, StringBuilder temp, char[] given, int index){
if(index==given.length){
return;
}
if(curr.children[given[index]-'a'] == null)return;
Trie next = curr.children[given[index] - 'a'];
temp.append(given[index]);
//If the word is present in the dictionary, we can either add a space or we continue going down in the trie
if(next.isWord){
//If we have reached the end of the string, add the value to Answer and terminate
if(index+1 == given.length){
ans.add(temp.toString());
temp.deleteCharAt(temp.length()-1);
return;
}
else{
//We are adding space
temp.append(" ");
//Since after adding space, the next letter should be the beginning of a new word from the dictionary, we pass in the ROOT trie node
search(ans, root, temp, given, index+1);
//remove the space we added in the above line.
temp.deleteCharAt(temp.length()-1);
}
}
//since we have not added a space, we are technically going down in the same trie block. So we are moving the NEXT node in the CURR trie node.
search(ans, next, temp, given, index+1);
//Deleting the added character
temp.deleteCharAt(temp.length()-1);
}
public List<String> wordBreak(String s, List<String> wordDict) {
root = new Trie();
exists = new int[26];
List<String> ans = new ArrayList();
for(String word : wordDict){
root.addWord(word);
}
for(char c : s.toCharArray()){
if(exists[c-'a']!=1)
return new ArrayList();
}
search(ans, root, new StringBuilder(), s.toCharArray(), 0);
return ans;
}
class Trie{
boolean isWord;
Trie[] children;
Trie(){
isWord=false;
children = new Trie[26];
}
public void addWord(String w){
Trie curr = this;
int index = 0;
char[] word = w.toCharArray();
while(index<word.length){
exists[word[index]-'a']=1;
if(curr.children[word[index]-'a'] == null){
curr.children[word[index]-'a'] = new Trie();
}
curr = curr.children[word[index]-'a'];
index++;
}
curr.isWord = true;
}
}
}