分词(使用动态编程:Top->Down)
给定一个字符串 s 和一个单词字典 dict,在 s 中添加空格来构造一个句子,其中每个单词都是一个有效的字典单词。返回所有这些可能的句子。
例如,给定
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"]。一个解决方案是[“猫和狗”,“猫沙狗”]。
问题:
- 时间复杂度?
- 空间复杂度?
我自己觉得,
- 时间复杂度 = O(n!),没有动态规划,n 是给定字符串的长度,
- 空间复杂度 = O(n)。
困惑的:
- 无法用动态规划计算时间复杂度。
- 看来上面的空间复杂度是不正确的。
代码[Java]
public class Solution {
public List<String> wordBreak(String s, Set<String> dict) {
List<String> list = new ArrayList<String>();
// Input checking.
if (s == null || s.length() == 0 ||
dict == null || dict.size() == 0) return list;
int len = s.length();
// memo[i] is recording,
// whether we cut at index "i", can get one of the result.
boolean memo[] = new boolean[len];
for (int i = 0; i < len; i ++) memo[i] = true;
StringBuilder tmpStrBuilder = new StringBuilder();
helper(s, 0, tmpStrBuilder, dict, list, memo);
return list;
}
private void helper(String s, int start, StringBuilder tmpStrBuilder,
Set<String> dict, List<String> list, boolean[] memo) {
// Base case.
if (start >= s.length()) {
list.add(tmpStrBuilder.toString().trim());
return;
}
int listSizeBeforeRecursion = 0;
for (int i = start; i < s.length(); i ++) {
if (memo[i] == false) continue;
String curr = s.substring(start, i + 1);
if (!dict.contains(curr)) continue;
// Have a try.
tmpStrBuilder.append(curr);
tmpStrBuilder.append(" ");
// Do recursion.
listSizeBeforeRecursion = list.size();
helper(s, i + 1, tmpStrBuilder, dict, list, memo);
if (list.size() == listSizeBeforeRecursion) memo[i] = false;
// Roll back.
tmpStrBuilder.setLength(tmpStrBuilder.length() - curr.length() - 1);
}
}
}