每日一题 day 38 (DP topic)

problem

139. Word Break
Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused multiple times in the segmentation.

Example 1:

Input: s = "leetcode", wordDict = ["leet","code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple","pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: false

wrong solution 1 Brute force recursive

class Solution {
public:
    bool recur(int start, string s, unordered_set<string>& word){
        if(start==s.size())return true;
        string sub;
        for(int i=start; i<s.size(); i++)
            if(word.count(sub+=s[i]) && recur(i+1, s, word)) 
                return true;
        return false;
    }
    
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> word(wordDict.begin(), wordDict.end());
        return recur(0, s, word);
    }
};

TLE

analysis

we analyze this solution with the example

  • s = abcd
  • set = a, b, c, bc, ab, abc

the time complexity depends on how many nodes the recursive tree has, and in the case mentioned above, the recursive tree is shown below.
在这里插入图片描述
from the following code we can see that

if(word.count(sub+=s[i]) && recur(i+1, s, word)) 

only if the word contains the prefix(such as a), the recursive tree can go down to the next level(such as bcd)

all the gray nodes with empty string cannot be reached because if the program reach one such node, it will immdiately return, and so that the remaining nodes on the right can not be reached.

so the conclusion is that for a string with length 4(abcd), the recursion tree has 8 nodes(the black nodes), so we can get a equation
8 = 2 4 − 1 8 = 2^{4-1} 8=241
so
O ( 2 n − 1 ) = O ( 2 n ) O(2^{n-1})=O(2^n) O(2n1)=O(2n)

solution 2 dp

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        vector<bool> dp(n+1, false); 
        unordered_set<string> word(wordDict.begin(), wordDict.end());
    
        dp[n]=true;
        for(int i=n-1; i>=0; i--){
            string sub;
            for(int j=i; j<n; j++){
                if(dp[i]=word.count(sub+=s[j]) && dp[j+1])
                    break;
            }
        }
        return dp[0];
    }
};

在这里插入图片描述
在这里插入图片描述

solution 3 DFS + prune

class Solution {
public:
    bool recur(int start, string s, unordered_set<string>& word, vector<char>& mem){
        int n = s.size();
        if(start==n)return true;
        if(mem[start] != -1)return mem[start];
        string sub;
        for(int i=start; i<n; i++)
            if(word.count(sub+=s[i]) && recur(i+1, s, word, mem)) 
                return mem[start]=1;
        return mem[start]=0;
    }
    
    bool wordBreak(string s, vector<string>& wordDict) {
        vector<char> mem(s.size(), -1);
        unordered_set<string> word(wordDict.begin(), wordDict.end());
        return recur(0, s, word, mem);
    }
};

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值