public class Solution {
public static Set<String> unmatch;
public boolean wordBreak(String s, Set<String> dict) {
unmatch = new HashSet<String>();
return helper(s,dict);
}
public boolean helper(String s,Set<String> dict){
if(s.length()<1)return true;
boolean flag = false;
for(int i=1;i<=s.length();i++){
String now = s.substring(0,i);
if(dict.contains(now)){
if(now.length()==s.length())return true;
String suffix = s.substring(i);
if(unmatch.contains(suffix))continue;
flag = wordBreak(s.substring(i),dict);
if(flag==false)unmatch.add(suffix);
}
if(flag)return true;
}
return false;
}
}