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

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

题目解析:

方案一个:

利用笨方法,长度一个一个增加,知道递归结束,将字符串放入容器中。这样做会超时。

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        if(s.size() == 0)
            return res;
        string store;
        FindBreak(s,0,store,dict);
        return res;
    }
    void FindBreak(string s,int index,string store,unordered_set<string> &dict){
        if(index >= s.size()){
            res.push_back(store);
            return ;
        }

        for(int i = 1;index+i<=s.size();i++){
            string word = s.substr(index,i);
            if(dict.find(word) != dict.end()){
                string tmp;
                if(store.size() == 0)
                    tmp = word;
                else
                    tmp = store+" "+word;
                FindBreak(s,index+i,tmp,dict);
            }
        }
    }
private:
    vector<string> res;
};


方案二:

跟上题一样,利用动态规划。由于要找到所有的路径情况,那么就不能设置一维数组,利用二维数组[i,j]表示这一段字符串是否在字典中。然后还是要递归。不过这里就不用查询字典了,直接判断动态规划数组,若为真就向下递归,若为假,就增加字符串长度。

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        if(s.size() == 0 || dict.size() == 0)
            return res;
        dp = new vector<bool>[s.size()];
        for(int i = 0;i < s.size();i++){
            for(int j = i;j < s.size();j++){
                dp[i].push_back(isMatch(s.substr(i,j-i+1),dict));
            }
        }
        FindBreak(s.size()-1,s);
        return res;
    }
    bool isMatch(string str,unordered_set<string> &dict){
        unordered_set<string> :: iterator it = dict.find(str);
        if(it != dict.end())
            return true;
        else
            return false;
    }
    void FindBreak(int i,string s){
        if(i>=0){
            for(int k = 0;k <= i;k++){
                if(dp[k][i-k]){ //如果k...i字符串在在字典中,构成一个完整字符串,就放入字符串集合
                    mystring.push_back(s.substr(k,i-k+1));
                    FindBreak(k-1,s);
                    mystring.pop_back();    //取出,再增加字符串长度进行尝试
                }
            }
        }else{
            string str;
            for(int i = mystring.size()-1;i >= 0;i--){  //当所有的情况都递归完了,再拼接
                str += mystring[i];
                if(i)
                    str.push_back(' ');
            }
            res.push_back(str);
        }
    }
private:
    vector<string> mystring;
    vector<string> res;
    vector<bool> *dp;
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值