单词拆分

给你一个字符串 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

提示:

1 <= s.length <= 300
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 20
s 和 wordDict[i] 仅有小写英文字母组成
wordDict 中的所有字符串 互不相同

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/word-break
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路1:dfs worddict中的所有组合

见注释。

code

class Solution {
public:
    //DFS 遍历所有的可能性
    //a tree of dfs

    bool dfs(string & s,vector<string> & wordDict,int start)
    {
        if(start >= s.size()) return true;

        for(int i = 0;i < wordDict.size();i ++)
        {
            int j;
            for(j = 0;j < wordDict[i].size();j ++)
            {
                if(s[start + j] == wordDict[i][j]) continue;
                else break;
            }
            if(j == wordDict[i].size())
            {
                if(dfs(s,wordDict,start + j)) return true;
            }

        }

        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        
        return dfs(s,wordDict,0);
    }
};

解题思路2 : 记忆化搜索优化

见注释

code

class Solution {
public:
    //DFS 遍历worddict所有组合的可能性
    //a tree of dfs
    //渐渐擅长写dfs
    //可惜超时了
    
    //由于回溯机制的存在,同一个start可能会被重复搜索,但是由于worddict相同,无论重复多少次结果都是相同的
    //只需要将之前遍历的一个start的结果存储下来,后面就可以避免重复的搜索

    bool dfs(string & s,vector<string> & wordDict,vector<int> & mem,int start)
    {
        if(start >= s.size()) return true;

        if(mem[start] != -1) return mem[start];

        for(int i = 0;i < wordDict.size();i ++)
        {
            int j;
            for(j = 0;j < wordDict[i].size();j ++)
            {
                if(s[start + j] == wordDict[i][j]) continue;
                else break;
            }
            if(j == wordDict[i].size())
            {
                if(dfs(s,wordDict,mem,start + j)) return true;
            }

        }

        mem[start] = false;
        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        vector<int> mem(s.size(),-1);
        return dfs(s,wordDict,mem,0);
    }
};

解题思路3:dfs 字符串的前缀

见注释

code

class Solution {
public:
    //换一种dfs的写法
    //遍历s的前缀判断是否在worddict中

    bool dfs(string & s,vector<string> & wordDict,int start)
    {
        if(start >= s.size()) return true;

        for(int i = start;i < s.size();i ++)
        {
            string prefix = s.substr(start,i - start + 1);
            if(find(wordDict.begin(),wordDict.end(),prefix) != wordDict.end() && dfs(s,wordDict,i + 1))
            {
                return true;
            }
        }
        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        
        return dfs(s,wordDict,0);
    }
};

解题思路4:记忆化搜索优化

见注释。

code

class Solution {
public:
    //换一种dfs的写法
    //遍历s的前缀判断是否在worddict中
    
    //时间复杂度:2 ^ n
    //时间复杂度高的原因是重复的结果被大量计算
    //比如aaaaaaab,wordict中存在一个a
    //那么start就会不断递增下去,直到碰到b
    //如果worddict有aa,start也会不断递增下去,由于使用的worddict是相同的,没有必要继续下去了,之前的start的不行,现在还是相同的start位置,相同的worddict也是不行
    //存储每一个start位置的结果即可

    bool dfs(string & s,vector<string> & wordDict,vector<int> & mem,int start)
    {
        if(start >= s.size()) return true;

        if(mem[start] != -1) return mem[start];

        for(int i = start;i < s.size();i ++)
        {
            string prefix = s.substr(start,i - start + 1);
            if(find(wordDict.begin(),wordDict.end(),prefix) != wordDict.end() && dfs(s,wordDict,mem,i + 1))
            {
                return true;
            }
        }
        mem[start] = false;
        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        vector<int> mem(n,-1);//-1 uninitialized 0 false  1 ture    
        return dfs(s,wordDict,mem,0);
    }
};

解题思路5:动态规划

见注释

code

class Solution {
public:
    //动态规划
    //状态表示:f[i]前i个字符是否可以划分为worddict中的单词
    //状态转移:f[j]且[j+1]->[i]是worddict中的单词
    //为了避免处理麻烦的边界条件,f[0] = true表示空字符串

    bool wordBreak(string s, vector<string>& wordDict) {
        
        int n = s.size();
        vector<bool> f(n + 1,false);
        
        f[0] = true;

        for(int i = 1;i <= n;i ++)
        {
            f[i] = false;
            for(int j = i;j > 0;j--)
            {
                if(f[j-1] && find(wordDict.begin(),wordDict.end(),s.substr(j-1,i - j + 1)) != wordDict.end() ) f[i] = true;
            }
        }

        return f[n];
    }
};
posted on 2023-03-12 12:55  huangxk23  阅读(82)  评论(0编辑  收藏  举报