[LeetCode] Word Break

简介: The typical solution to this problem is to use Dynamic Programming. The state dp[i] represents whether s[0.

The typical solution to this problem is to use Dynamic Programming. The state dp[i] represents whether s[0..i - 1] can be broken into words in wordDict and the state equation is:

dp[i] = true, if there exists j < i such that dp[j] = true and s.substr(j, i - j) is in wordDict

dp[i] = false, otherwise.

The code is as follows (we use minlen and maxlen to reduce the number of checks).

 1 class Solution {
 2 public:
 3     bool wordBreak(string s, unordered_set<string>& wordDict) {
 4         int n = s.length();
 5         vector<bool> dp(n + 1, false);
 6         dp[0] = true;
 7         for (string word : wordDict) {
 8             minlen = min(minlen, (int)word.length());
 9             maxlen = max(maxlen, (int)word.length());
10         }
11         for (int i = 1; i <= n; i++) {
12             for (int j = i - minlen; j >= max(0, j - maxlen); j--) {
13                 if (dp[j] && wordDict.find(s.substr(j, i - j)) != wordDict.end()) {
14                     dp[i] = true;
15                     break;
16                 }
17             }
18         }
19         return dp[n];
20     }
21 private:
22     int minlen, maxlen;
23 };

This problem can also be solved using DFS or BFS.

DFS:

 1 class Solution {
 2 public:
 3     bool wordBreak(string s, unordered_set<string>& wordDict) {
 4         for (string word : wordDict) {
 5             minlen = min(minlen, (int)word.length());
 6             maxlen = max(maxlen, (int)word.length());
 7         }
 8         unordered_map<string, bool> records;
 9         return wordBreakDFS(s, 0, wordDict, records);
10     }
11 private:
12     int minlen, maxlen;
13     bool wordBreakDFS(string& s, int idx, unordered_set<string>& wordDict, unordered_map<string, bool>& records) {
14         int n = s.length();
15         if (idx == n) return true;
16         string tail = s.substr(idx, n - idx);
17         if (records.find(tail) != records.end())
18             return records[tail];
19         for (int i = idx + minlen; i <= min(idx + maxlen, n); i++) {
20             string part = s.substr(idx, i - idx);
21             if (wordDict.find(part) != wordDict.end() && wordBreakDFS(s, i, wordDict, records))
22                 return records[part] = true;
23         }
24         return records[tail] = false;
25     }
26 };

BFS:

 1 class Solution {
 2 public:
 3     bool wordBreak(string s, unordered_set<string>& wordDict) {
 4         int n = s.length();
 5         for (string word : wordDict) {
 6             minlen = min(minlen, (int)word.length());
 7             maxlen = max(maxlen, (int)word.length());
 8         }
 9         queue<int> toVisit;
10         unordered_set<int> visited;
11         toVisit.push(0);
12         while (!toVisit.empty()) {
13             int start = toVisit.front();
14             toVisit.pop();
15             if (visited.find(start) == visited.end()) {
16                 visited.insert(start);
17                 for (int end = start + minlen - 1; end < min(start + maxlen, n); end++) {
18                     string part = s.substr(start, end - start + 1);
19                     if (wordDict.find(part) != wordDict.end()) {
20                         if (end + 1 == n) return true;
21                         toVisit.push(end + 1);
22                     }
23                 }
24             }
25         }
26         return false;
27     }
28 private:
29     int minlen, maxlen;
30 };

 

目录
相关文章
LeetCode 343. Integer Break
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
74 0
LeetCode 343. Integer Break
LeetCode 318. Maximum Product of Word Lengths
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
77 0
LeetCode 318. Maximum Product of Word Lengths
LeetCode 290. Word Pattern
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。
47 0
LeetCode 290. Word Pattern
LeetCode 212. Word Search II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
79 0
LeetCode 212. Word Search II
|
索引
LeetCode 79. Word Search
给定一个2D板和一个单词,找出该单词是否存在于网格中。 该单词可以由顺序相邻的单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。 相同的字母单元格不得多次使用。
73 0
LeetCode 79. Word Search
|
Python
[Leetcode][Python]Word Break/Word Break II/单词拆分/单词拆分 II
Word Break 题目大意 给定一个目标字符串和一组字符串,判断目标字符串能否拆分成数个字符串,这些字符串都在给定的那组字符串中。 解题思路 动态规划
127 0
|
索引 存储
LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611509 翻译 给定一个模式,和一个字符串str,返回str是否符合相同的模式。
956 0