Skip to content

Commit a1d762c

Browse files
authored
Create [Optimized]WordBreakPrintWords.java
1 parent 8539449 commit a1d762c

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
https://leetcode.com/problems/word-break-ii/submissions/
3+
*/
4+
5+
class Solution {
6+
public List<String> wordBreak(String s, List<String> wordDict) {
7+
if(s.length()==0 || s==null) return new ArrayList<String>();
8+
9+
int maxLen = -1;
10+
//to find the length of the longest word in the dictionary
11+
//will be used to minimize iterations
12+
for(int i=0 ; i < wordDict.size() ; i++){
13+
if(maxLen < wordDict.get(i).length()) maxLen = wordDict.get(i).length();
14+
}
15+
16+
//Converting List to Set as retieval from set takes place in constant time
17+
Set<String> dic = new HashSet<String>(wordDict);
18+
19+
//to store solutions to already seen subproblems
20+
HashMap<Integer, ArrayList<String>> map = new HashMap<>();
21+
22+
//helper function
23+
return dfs(s,0,maxLen,dic,map);
24+
25+
}
26+
27+
public List<String> dfs(String s, int start, int maxLen, Set<String> dict, HashMap<Integer, ArrayList<String>> map){
28+
29+
//if subproblem is already solved return the stored solution
30+
if(map.containsKey(start)) return map.get(start);
31+
32+
//list to store solutions to the current subproblem
33+
ArrayList<String> res = new ArrayList<String>();
34+
35+
//if we have exhausted the entire string
36+
if(start == s.length()) res.add("");
37+
38+
/*
39+
the current subproblem starts at the index 'start'
40+
instead of considering the string s[start..... s.length()-1]
41+
we consider the string ending at minimum of(s.length(), start+maxLen)
42+
Logic - If want to check if current prefix s[start...end] is a word in the dictionary or not - If it is a valid word
43+
it can have a ( maximum length <= length of the longest word in the dictionary ) - considering lengths larger than
44+
this value is simply a waste of iterations, as no such word exisists in the dictionary.
45+
However, there might be a case when we are near the end of the string and [start+maxLen > s.length()] in such case
46+
the upper bound becomes s.length() ==> we take minimum of the two ==> Math.min(s.length(), start+maxLen)
47+
*/
48+
49+
//considers all possible prefixes
50+
for(int end=start ; end < Math.min(s.length(), start+maxLen) ; end++){
51+
52+
String prefix = s.substring(start, end+1);
53+
54+
//check if the prefix is a valid word
55+
if(dict.contains(prefix)){
56+
57+
//solve the subproblem - break down the suffix
58+
//broken suffix is returned - possible to break into multiple valid results - hence a list is returned
59+
List<String> brokenSuffix = dfs(s,end+1,maxLen,dict,map);
60+
61+
//for each valid broken suffix add the current prefix and store it in the result
62+
for(String suff : brokenSuffix){
63+
if(suff.length()==0) res.add(prefix); //current prefix is actually the last valid suffix
64+
else res.add(prefix + " " + suff);
65+
}
66+
}
67+
}
68+
69+
//store result to current subproblem
70+
map.put(start, res);
71+
72+
//return result to current subproblem
73+
return res;
74+
75+
}
76+
}

0 commit comments

Comments
 (0)