[LeetCode] Word Break II

简介: Well, it seems that many people meet the TLE problem. Well, I use a simple trick in my code to aoivd TLE.

Well, it seems that many people meet the TLE problem. Well, I use a simple trick in my code to aoivd TLE. That is, each time before I try to break s, I call isBreakable (just wordBreak from Word Break) to see whether it is breakable. If it is not breakable, then we need not move on. Otherwise, we break it using backtracking.

The idea of backtracking is also typical. Start from index 0 of s, each time we see a word that is in wordDict, add it to a temporary string sentence. When we reach the end of the s, add sentence to sentences. Then we need to recover sentence back to its original status before word was added. So I simply record a temporary string temp each time before I add word to sentence and use it to recover the status.

The code is as follows. I hope it to be self-explanatory enough.

 1 class Solution {
 2 public:
 3     bool isWordBreak(string& s, unordered_set<string>& wordDict) {
 4         vector<bool> dp(s.length() + 1, false);
 5         dp[0] = true;
 6         int minlen = INT_MAX;
 7         int maxlen = INT_MIN;
 8         for (string word : wordDict) {
 9             minlen = min(minlen, (int)word.length());
10             maxlen = max(maxlen, (int)word.length());
11         }
12         for (int i = 1; i <= s.length(); i++) {
13             for (int j = i - minlen; j >= max(0, i - maxlen); j--) {
14                 if (dp[j] && wordDict.find(s.substr(j, i - j)) != wordDict.end()) {
15                     dp[i] = true;
16                     break;
17                 }
18             }
19         }
20         return dp[s.length()];
21     }
22     void breakWords(string s, int idx, unordered_set<string>& wordDict, string& sol, vector<string>& res) {
23         if (idx == s.length()) {
24             sol.resize(sol.length() - 1);
25             res.push_back(sol);
26             return;
27         }
28         for (int i = idx; i < s.length(); i++) {
29             string word = s.substr(idx, i - idx + 1);
30             if (wordDict.find(word) != wordDict.end()) {
31                 string tmp = sol;
32                 sol += word + " ";
33                 breakWords(s, i + 1, wordDict, sol, res);
34                 sol = tmp;
35             }
36         }
37     }
38     vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
39         string sol;
40         vector<string> res;
41         if (!isWordBreak(s, wordDict)) return res;
42         breakWords(s, 0, wordDict, sol, res);
43         return res;
44     }
45 };

 

目录
相关文章
LeetCode 343. Integer Break
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
76 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 中的每个非空单词之间存在着双向连接的对应模式。
49 0
LeetCode 290. Word Pattern
LeetCode 212. Word Search II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
79 0
LeetCode 212. Word Search II
|
索引
LeetCode 79. Word Search
给定一个2D板和一个单词,找出该单词是否存在于网格中。 该单词可以由顺序相邻的单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。 相同的字母单元格不得多次使用。
74 0
LeetCode 79. Word Search
|
Python
[Leetcode][Python]Word Break/Word Break II/单词拆分/单词拆分 II
Word Break 题目大意 给定一个目标字符串和一组字符串,判断目标字符串能否拆分成数个字符串,这些字符串都在给定的那组字符串中。 解题思路 动态规划
128 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