Skip to content

Commit 8539449

Browse files
authored
Create WordBreakPrintWords.java
1 parent 7c527ec commit 8539449

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
https://www.techiedelight.com/word-break-problem/
3+
https://www.geeksforgeeks.org/word-break-problem-using-backtracking/?ref=rp
4+
*/
5+
6+
public void printWordBreak(String s, Set<String> dict, String res){
7+
if(s.length() == 0){
8+
System.out.println(res);
9+
return;
10+
}
11+
12+
for(int i=1;i<=s.length();i++){
13+
String prefix = s.substring(0,i);
14+
15+
if(dict.contains(prefix)){
16+
printWordBreak(s.substring(i), dict, res+" "+prefix);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)