-
Notifications
You must be signed in to change notification settings - Fork 1
/
WordBreak.java
115 lines (96 loc) · 3.19 KB
/
WordBreak.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Problem : https://www.geeksforgeeks.org/word-break-problem-trie-solution/
* https://www.techiedelight.com/word-break-problem-using-trie/
*/
import java.util.Map;
import java.util.HashMap;
// Class to represent trie node
class TrieNode {
boolean isLeaf; // mark the leaf node also the end of the string key
Map<Character, TrieNode> next; // mpa to store the children of node
public TrieNode() {
isLeaf = false;
next = new HashMap<>();
}
}
// Class to represent the trie
class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Return root node
public TrieNode getRoot() {
return root;
}
// Iteratively insert new key in trie
public void insert(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
TrieNode current = root;
for (int i = 0; i < key.length(); ++i) {
char c = key.charAt(i);
current.next.putIfAbsent(c, new TrieNode());
current = current.next.get(c);
}
current.isLeaf = true;
}
// Iteratively search key in trie
public boolean search(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
TrieNode current = root;
for (int i = 0; i < key.length(); ++i) {
char c = key.charAt(i);
if (current.next.get(c) == null) {
return false;
}
current = current.next.get(c);
}
return current.isLeaf; // search is successful only if this is a leaf node
}
}
// Class to operate on word break using trie data structure
public class WordBreak {
// Recursively find out if breaking word is possible or not
private static boolean wordBreakRec(Trie trie, String word) {
// If word is exhausted or a of 0 length
if (word.length() == 0) {
return true;
}
// Recursively break the word into 2 parts
// 1. Prefix of word till ith length. Check if this prefix is present in trie
// 2. Remaining subsring from ith till end. Recursovely solve this problem
for (int i = 1; i <= word.length(); ++i) {
if (trie.search(word.substring(0, i)) &&
WordBreak.wordBreakRec(trie, word.substring(i))) {
return true;
}
}
// Tried all prefixes but nothing break not possible
return false;
}
// Helper function to call actual function
public static boolean wordBreak(Trie trie, String word) {
if (trie == null || word == null) {
throw new IllegalArgumentException();
}
return wordBreakRec(trie, word);
}
// Test code
public static void main(String[] args) {
String dictionary[] = {"mobile","samsung","sam",
"sung","man","mango",
"icecream","and","go","i",
"like","ice","cream"};
Trie trie = new Trie();
for (String w : dictionary) {
trie.insert(w);
}
String word = "ilikemango";
boolean possible = WordBreak.wordBreak(trie, word);
System.out.println(possible);
}
}