力扣139.单词拆分I & II

单词拆分I

题目:
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

说明:
拆分时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。

示例 1:

输入: s = “leetcode”, wordDict = [“leet”, “code”]
输出: true
解释: 返回 true 因为 “leetcode” 可以被拆分成 “leet code”。

示例 2:

输入: s = “applepenapple”, wordDict = [“apple”, “pen”]
输出: true
解释: 返回true 因为 “applepenapple” 可以被拆分成 “apple pen apple”。注意你可以重复使用字典中的单词。

示例 3:

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

  • 思路一(超时)
    比较容易想到的解法是,从字符串 s 开头处开始匹配,若匹配到字典里的字符串,则继续匹配剩下的字符串。这样递归下去,直到找到一个可行的序列或尝试所有的单词组合。

代码:

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        if(s.length() == 0) {
            return true;
        }
        else {
            for(int i = 1; i <= s.length(); i++)  {
                string subs = s.substr(0, i);
                if(find(wordDict.begin(), wordDict.end(), subs) != wordDict.end()) { 
                    if(wordBreak(s.substr(i,s.length()), wordDict)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
};

输出超时:
在这里插入图片描述

  • 思路二
    上述思路一的做法会造成大量冗余,我们其实并不关心如何用几种方式进行切分,只是关心能否切分。
    新的思路是:新建一个数组vector isbreak(n, false),每个位置记录下该位置之前的子串是否可以被分割,这样只需要记录每个位置之前的子串能否被切分就好了。这样我们判断当前位置i之前能否被切分时,只需要判断从i之前所有可以被切分的位置j到i的子串能否在字典中找到即可,即:
if(isbreak[j] && find(wordDict.begin(), wordDict.end(), s.substr(j,i-j)) != wordDict.end()) {
                    isbreak[i] = true;
                }

代码:

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.length();
        vector<bool> isbreak(n, false);
        isbreak[0] = true;
        for(int i = 1; i <= n; i++) {
            for(int j = 0; j <= i; j++) {
                if(isbreak[j] && find(wordDict.begin(), wordDict.end(), s.substr(j,i-j)) != wordDict.end()) {
                    isbreak[i] = true;
                }
            }
        }
        return isbreak[n];
    }
};

在这里插入图片描述

——————————————————————————————————

单词拆分 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”]
输出: []

解答
在单词拆分I的基础上进行动态规划,和以往vector使用push_back()、pop_back()的回溯方式不同,这里巧妙的使用了将原str赋值给新的nextstr后再进行append(),之后str还是原先的str无需回溯。动态规划部分代码如下:

void dfs(vector<string>& result, string s, string str, vector<string>& wordDict) {
        if(s.length() == 0){
            result.push_back(str);
        }
        else {
            for(int i = 1; i <= s.length(); i++)  {
                string subs = s.substr(0, i);
                if(find(wordDict.begin(), wordDict.end(), subs) != wordDict.end()) { 
                    string nextstr = str;
                    if(nextstr.length()) {
                        nextstr.append(" ");
                    }
                    nextstr.append(subs);
                    dfs(result, s.substr(i, s.length()-i), nextstr, wordDict);
                }
            }
        }
    }

总代码:

class Solution {
public:
    vector<string> wordBreak(string s, vector<string>& wordDict) {
        int n = s.length();
        vector<bool> isbreak(n+1, false);
        isbreak[0] = true;
        for(int i = 1; i <= n; i++) {
            for(int j = 0; j <= i; j++) {
                if(isbreak[j] && find(wordDict.begin(), wordDict.end(), s.substr(j,i-j)) != wordDict.end()) {
                    isbreak[i] = true;
                }
            }
        }
        
        vector<string> result;
        if(isbreak[n]){
            string str;
            dfs(result, s, str, wordDict);
        }
        return result;
    }
    
    void dfs(vector<string>& result, string s, string str, vector<string>& wordDict) {
        if(s.length() == 0){
            result.push_back(str);
        }
        else {
            for(int i = 1; i <= s.length(); i++)  {
                string subs = s.substr(0, i);
                if(find(wordDict.begin(), wordDict.end(), subs) != wordDict.end()) { 
                    string nextstr = str;
                    if(nextstr.length()) {
                        nextstr.append(" ");
                    }
                    nextstr.append(subs);
                    cout << i << " " << nextstr << endl;
                    dfs(result, s.substr(i, s.length()-i), nextstr, wordDict);
                }
            }
        }
    }
};

输出:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值