-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion139.java
More file actions
53 lines (44 loc) · 1.12 KB
/
Copy pathQuestion139.java
File metadata and controls
53 lines (44 loc) · 1.12 KB
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
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 139
*/
import java.util.*;
public class Question139
{
public static void add(String s, List<String> wordDict, StringBuilder sb, boolean[] result)
{
System.out.println(sb);
for(String word:wordDict)
{
int startingPosition = sb.length();
sb.append(word);
if(sb.length()<s.length())
add(s,wordDict,sb,result);
else if(sb.length()==s.length())
{
if(s.equals(sb.toString()))
result[0] = true;
}
//Remove the appended word from the sb object
sb.delete(startingPosition,sb.length());
}
}
public static boolean wordBreak(String s, List<String> wordDict)
{
boolean[] result = new boolean[1];
result[0] = false;
StringBuilder sb = new StringBuilder();
add(s,wordDict,sb,result);
return result[0];
}
public static void main(String[] args)
{
String s = "applepenappleapple";
List<String> wordDict = new ArrayList<>();
wordDict.add("apple");
wordDict.add("pen");
boolean result = wordBreak(s,wordDict);
System.out.println(result);
}
}