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".
DP很容易就出来了。possible[i]保存[0,i]是否可以被分割的结果。
possible[i] = true, 当存在possible[k] = true,且[k,i]是dict里的一个word时。否则possible[i]
= false。
这种是自底而下的。
1 class Solution {
2 public:
3 bool wordBreak(string s, unordered_set<string> &dict) {
4 int n = s.length();
5 if (n == 0) return true;
6 vector<bool> possible(n, false);
7
8 for (int i = 0; i < n; ++i) {
9 for (int j = i; j >= 0; --j) {
10 if ((j == 0 || possible[j - 1]) && dict.find(s.substr(j, i - j + 1)) != dict.end()) {
11 possible[i] = true;
12 break;
13 }
14 }
15 }
16
17 return possible[n - 1];
18 }
19 };
算法的时间复杂度最坏情况是O(n^2),空间复杂度是O(n)。
网上也有人用前缀树(Trie树、字典树)实现的。私以为用前缀树还得先将dict里的所有word插进去,时间复杂度为O(n*l+s),l为word的最大长度,s为dict的大小。如果dict的大小比n大得多,那么整个开销也是不菲的。
只要稍微将上面的代码优化一下,先求出word的最大长度,那么时间复杂度也可以优化到O(n*l+s)。
Leetcode | Work Break,码迷,mamicode.com
时间: 2024-08-29 01:06:32