【LeetCode】139. Word Break

139. Word Break

Description:
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Difficulty:Medium
Example:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
方法1:深度搜索,超时
  • Time complexity : O ( ) O\left (\right ) O()
  • Space complexity : O ( ) O\left ( \right ) O()
    思路:
    每次找一个能匹配的segment,然后递归剩下的s以及全部dict,这里有一个技巧是要用每次return的|的结果,因为只要有一个true就ok。
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        return helper(s, 0, s.size()-1, wordDict);
    }
    bool helper(string &s, int l, int r, vector<string>& dict){
        if(l == r+1) return true;
        bool res = false;
        for(int i = 0; i < dict.size(); i++){
            if(r-l+1 >= dict[i].size()){
                int j = 0;
                for(;j < dict[i].size(); j++){
                    if(s[l+j] != dict[i][j])
                        break;
                }
                if(j == dict[i].size()) 
                    res |= helper(s, l+j, r, dict);
            }
            else{
                continue;
            }
        }
        return res;
    }
};
方法2:动态规划
  • Time complexity : O ( ) O\left (\right ) O()
  • Space complexity : O ( ) O\left ( \right ) O()
    思路:
    dp[i] = {...|...dp[j] && s[j:i] in dict... |...} 0<=j<i大括号内相与
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        if(wordDict.size() == 0) return false;
        unordered_set<string> dict;
        for(auto w: wordDict)
            dict.insert(w);
        vector<bool> dp(s.size()+1, false);
        dp[0] = true;
        for(int i = 1; i <= s.size(); i++){
            for(int j = i-1; j >= 0; j--){
                if(dp[j]){
                    string segment = s.substr(j, i-j);
                    if(dict.find(segment) != dict.end()){
                        dp[i] = true;
                        break;
                    }
                }
            }
        }
        return dp[s.size()];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值