Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18c377d

Browse files
author
Tushar Roy
committed
Update word break
1 parent 8a98d0b commit 18c377d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

‎src/com/interview/dynamic/BreakMultipleWordsWithNoSpaceIntoSpace.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ private boolean wordBreakTopDownOneSolutionUtil(String s, Set<String> dict, int
181181
return false;
182182
}
183183

184+
public boolean wordBreakBottomUp(String s, List<String> wordList) {
185+
boolean[] T = new boolean[s.length() + 1];
186+
Set<String> set = new HashSet<>();
187+
for (String word : wordList) {
188+
set.add(word);
189+
}
190+
T[0] = true;
191+
for (int i = 1; i <= s.length(); i++) {
192+
for (int j = 0; j < i; j++) {
193+
if(T[j] && set.contains(s.substring(j, i))) {
194+
T[i] = true;
195+
break;
196+
}
197+
}
198+
}
199+
return T[s.length()];
200+
}
184201

185202
public static void main(String args[]){
186203
Set<String> dictionary = new HashSet<String>();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /