Leetcode | Work Break

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

Leetcode | Work Break的相关文章

LeetCode——Word Break

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: Word Break [139]

[题目] 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 be

LeetCode: Word Break II [140]

[题目] 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", "cat

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: 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, givens = "c

LeetCode: Word Break I &amp;&amp; II

I title: 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, givens = "leetcode",dict = ["leet", "code"]. Return true

[LeetCode]Word Break 特里

意甲冠军:推断字符串给定的字符串是否构成词典. 来推断目标字符串相匹配整个字典.我们需要来推断目标字符串的每个前缀开始的下一场比赛,这需要匹配目标字符串的成功,所有前缀的枚举. class TrieNode{//from http://www.cnblogs.com/x1957/p/3492926.html public: TrieNode* ch[26];//char指针数组 bool isWord; TrieNode():isWord(false){ memset(ch,0,sizeof(T

LeetCode -- Word Break 动态规划,详细理解

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] Word Break 拆分词句

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, givens = "leetcode",dict = ["leet", "code"]. Return true because &

[Leetcode] word break 拆分词语

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, givens ="leetcode",dict =["leet", "code"]. Return true because&quo