每日一题之单词拆分II

2020-04-22  本文已影响0人  this_is_for_u

题目140:单词拆分II

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

示例1:

输入:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
[
  "cats and dog",
  "cat sand dog"
]

示例2:

输入:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
输出:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
解释: 注意你可以重复使用字典中的单词。

示例3:

输入:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
输出:
[]

分析

可以动态规划也可以回溯方法,这里还是使用回溯方法,但如果普通的回溯方法会超时,这里需要做一些优化。

代码

class Solution {
public:
    vector<string> wordBreak(string s, vector<string>& wordDict) {
        GeneSets(wordDict); // vector convert to set
        return BackTrace(s, 0);
    }

    vector<string> BackTrace(string &s, int idx) {
        if (maps.find(idx) != maps.end()) { // 避免不必要的递归
            return maps[idx];
        }
        int s_size = s.size();
        vector<string> ret;
        if (idx == s_size) {
            ret.emplace_back("");
            return ret;
        }
        for (int i = min_len; i <= max_len; ++i) {
            if (idx + i > s_size) { break; }
            // 避免不必要的字符串截取
            if (len_sets.find(i) == len_sets.end()) { continue; }
            string tem = s.substr(idx, i);
            if (str_sets.find(tem) != str_sets.end()) {
                vector<string> tem_ret = BackTrace(s, idx + i);
                maps[idx+i] = tem_ret; // 保存每次计算的结果
                for (auto &tem_str : tem_ret) {
                    string ss = (tem_str == "") ? "" : (" " + tem_str);
                    ret.emplace_back(tem + ss);
                }
            }
        }
        return ret;
    }

    map<int, vector<string>> maps;
    set<string> str_sets;
    set<int> len_sets;
    int min_len = INT_MAX;
    int max_len = 0;
    void GeneSets(vector<string>& wordDict) {
        for (auto &s : wordDict) {
            int size = s.size();
            min_len = min(min_len, size);
            max_len = max(max_len, size);
            str_sets.emplace(s);
            len_sets.emplace(size);
        }
    }
};

更多文章请关注

代码精进之路
上一篇 下一篇

猜你喜欢

热点阅读