[LeetCode] Word Break II (TLE)

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", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

这题没有AC,但是做出了递归回朔的版本,最后一个一串a后面跟个b的case超时了没法通过。

sand -- [dog] ->"cat sand dog"

/

cat --[sanddog]

/

[catsanddog]

\

cats --[anddog]

\

and -- [dog] -> "cats and dog"

从这个结构可以看出,这个问题可以被分解为当前的问题+更小规模的同类子问题,用递归求解。

我用了一个vector<stirng>来记录每个阶段(路径)上的结果,当搜索完字符串的时候(left >= right)意味着数组里的就是一个结果。

void wb(string s, int left, int right, unordered_set<string> &dict, vector<string> t,  vector<string> &re) {
    if (s.length() <= 0)
        return;
    if (left >= right) {
        string k;
        for (int i=0; i<t.size(); i++) {
            if (i != t.size()-1) {
                k = k + t[i] + " ";
            }
            else
                k+=t[i];
        }
        re.push_back(k);
        return;
    }
    //find matches
    for (const auto& elem: dict) {
        int len = (int)((string)elem).size();
        if (left+len > s.length()) continue;
        string sub = s.substr(left, len);
        if (elem.compare(sub) == 0) {
            t.push_back(sub);
            wb(s, left+len, right, dict, t, re);
            if (t.size())
                t.pop_back();
        }
    }
}

vector<string> wordBreak(string s,  unordered_set<string> &dict) {
    vector<string> ret;
    vector<string> t;
    wb(s, 0, s.size()-1, dict,t, ret);
    return ret;
}

在编写过程中遇到的两个bug都是因为没有很好理解程序造成的,第一个是t数组,一开始我传起引用,结果发现最后结果集翻倍。。。 第二个是调用wb后 t 没有pop,这样就把前一个分支的内容带到了下一分支造成错乱。

求大神指导如何AC

时间: 2024-10-17 09:46:41

[LeetCode] Word Break II (TLE)的相关文章

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 II 解题报告

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, given s = "catsanddog", dict = ["cat",

[LeetCode] 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 = "catsanddog",dict = ["cat", "cats"

[Leetcode] 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 ="catsanddog",dict =["cat", "cats",

[LeetCode] 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, given s = "catsanddog", dict = ["cat", "cats&quo

【leetcode】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 = "catsanddog",dict = ["cat", "cats"

Word Break II 求把字符串拆分为字典里的单词的所有方案 @LeetCode

这道题类似  Word Break 判断是否能把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不仅仅是是否能拆分,而是要求出所有的拆分方案.因此用递归. 但是直接递归做会超时,原因是LeetCode里有几个很长但是无法拆分的情况,所以就先跑一遍Word Break,先判断能否拆分,然后再进行拆分. 递归思路就是,逐一尝试字典里的每一个单词,看看哪一个单词和S的开头部分匹配,如果匹配则递归处理S的除了开头部分,直到S为空,说明可以匹配. public class Solution

Word Break II leetcode java

题目: 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", "cats