1 public class Solution { 2 public boolean wordBreak(String s, Set<String> wordDict) { 3 int len = s.length(); 4 boolean[] isValidStart = new boolean[len+1]; 5 isValidStart[0] = true; 6 7 for(int i = 0; i < len; i++) { 8 if(!isValidStart[i]) { 9 continue; 10 } 11 for(int j = i + 1; j <= len; j++) { 12 String sub = s.substring(i, j); 13 if(wordDict.contains(sub)) { 14 isValidStart[j] = true; 15 if(isValidStart[len]) { 16 return true; 17 } 18 } 19 } 20 } 21 22 return false; 23 } 24 }