LeetCode C++ 139. Word Break【Dynamic Programming】中等

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.

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

题意:判断给出的字符串是否可以被 wordDict 中的字符串拼凑出来。

思路:动态规划的典型题目。

代码:

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wd(wordDict.begin(), wordDict.end());
        vector<bool> dp(s.size() + 1, false);
        //dp[i]表示s的前i个字符可以被wordDict拼凑出来
        //dp[0]即s的前0个字符"", 为true
        dp[0] = true;
        //每次循环, 检查长度为i的字符串区间是否可以被拼凑出来,直到检查整个s
        for (int i = 1; i <= s.size(); ++i) {
            //每次循环, 若前面长j的字符串可以被拼出来, 则检查从j开始的长i区间
            //的后一部分长为i-j的字符串是否可以在wordDict中找到
            //接着依次检查长为j+1,j+2,...,i-1的字符串是否可以被拼出来,...
            for (int j = 0; j < i; ++j) {
                if (dp[j] && wd.find(s.substr(j, i - j)) != wd.end()) {
                    //前面的长j字符串和后面长i-j的字符串均可拼出
                    //则s的前i个字符都可拼凑, dp[i]为true
                    dp[i] = true; 
                    break;
                }
            }
        }
        return dp[s.size()];
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值