Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
一看这题目,就感觉里面回溯法,因为这种题,在回溯法中在平常不过了。上代码:
public boolean wordBreak(String s, Set<String> wordDict) { if (s == null || wordDict == null) return false; return dfs(s, wordDict, 0); } private boolean dfs(String str, Set<String> wordDict, int s) { if (s > str.length()) return false; else if (s == str.length()) return true; for (int i = s+1; i <= str.length(); i++) { String sub = str.substring(s, i); if (wordDict.contains(sub) && dfs(str, wordDict, i)) { return true; } } return false; }
看一看递归树:
a aa aaa aaab
a aa aab a ab b
a ab b b
b
可以看出重叠子问题,因为在没有找到匹配的时候,会全部遍历这课树,所以可以改用动态规划。
boolean[] can = new boolean[s.length()+1]; can[0] = true;// 空字符串 //f(0, i) = f(0, j) & f(j, i) k = 0...n-1; i = 0...n for (int i = 1; i <= s.length(); i++) { for (int j = 0; j < i; j++) { if (can[j] && wordDict.contains(s.substring(j, i))) { can[i] = true; break; } } } return can[s.length()];
public boolean wordBreak(String s, Set<String> wordDict) { boolean[] can = new boolean[s.length()+1]; can[s.length()] = true; for (int i = s.length()-1; i >= 0; i--) { for (int j = s.length(); j > i; j--) { if (wordDict.contains(s.substring(i, j)) && can[j]) { can[i] = true; break; } } } return can[0]; }
接下来搞word breakII
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
因为要列出所有的可能,所以肯定要用dfs,一个很标准额DFS模板式解答:
public List<String> wordBreak(String s, Set<String> wordDict) { List<String> list = new ArrayList<String>(); if (s == null || wordDict == null) return list; dfs(list, s, wordDict, new StringBuilder(), 0); return list; } private void dfs(List<String> list, String s, Set<String> dict, StringBuilder sb, int index) { if (index == s.length()) { list.add(new String(sb.toString())); return; } for (int i = index+1; i <= s.length(); i++) { StringBuilder temp = new StringBuilder(sb); String sub = s.substring(index, i); if (dict.contains(sub)) { if (index != 0) temp.append(" "); temp.append(sub); dfs(list, s, dict, temp, i); } } }
public List<String> wordBreak(String s, Set<String> wordDict) { List<String> list = new ArrayList<String>(); if (s == null || wordDict == null) return list; dfs(list, s, wordDict, new StringBuilder(), check(s, wordDict), 0); return list; } private void dfs(List<String> list, String s, Set<String> dict, StringBuilder sb, boolean[] can, int index) { if (index == s.length()) { list.add(new String(sb.toString())); return; } for (int i = index+1; i <= s.length(); i++) { String sub = s.substring(index, i); StringBuilder temp = new StringBuilder(sb); if (dict.contains(sub) && can[i]) { if (index != 0) temp.append(" "); temp.append(sub); dfs(list, s, dict, temp, can, i); } } } private boolean[] check(String s, Set<String> wordDict) { boolean[] can = new boolean[s.length()+1]; can[s.length()] = true; for (int i = s.length()-1; i >= 0; i--) { for (int j = s.length(); j > i; j--) { if (wordDict.contains(s.substring(i, j)) && can[j]) { can[i] = true; break; } } } return can; }