The remedy is: using a hashSet to remember impossible breaking points which have been tried.
public class Solution { public boolean wordBreak(String s, Setdict) { return recursion(s, dict, 0); } public boolean recursion(String s, Set dict, int current){ if(current == s.length()) return true;//base case for(int i = s.length(); i>current; i--){ if(dict.contains(s.substring(current, i))) { dict.add(s.substring(0, i)); if(recursion(s, dict, i)) return true; } } return false; } }
No comments:
Post a Comment