LeetCode 139 Word Break(Medium)

查阅更多的题解,请点击

Problem

139. Word Break(Medium)

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

Solution

设s的长度为n,字典的大小为m

O ( n 2 + m ) O(n^2+m) O(n2+m) time

先观察问题是否可以reduce成较小的问题,这里直接求解长度为n的字符串较难,转而k考虑前k个字符,定义子问题

  • 前k个元素组成的字符串是否可以根据字典中的单词break

对于这样的子问题,如何迭代求解?令dp[k]代表前k个元素是否可以根据字典中的单词break:若可以,则dp[k]=true;否则dp[k]=false;

  • 若dp[k-1]=true, 判断从k-1到k的子串是否在字典中
    • 若在,dp[k]=true, 转到dp[k+1]
    • 若不在,转到dp[k-2]
  • 若dp[k-1]=false, 转到dp[k-2]

考虑字典中单词的最短和最长长度,可以忽略过程中一些没有必要的查找过程(eg:当前子串的长度小于最短或长于最长长度,肯定不在字典中存在)

该过程是一个动态规划的过程,最差情况下的时间复杂度: O ( n 2 + m ) O(n^2+m) O(n2+m) time,代码如下:


class Solution
{
public:
    bool wordBreak(string s, vector<string> &wordDict)
    {
        unordered_set<string> dicts;
        int minLength = INT_MAX, maxLength = 0;
        for (auto item : wordDict)
        {
            dicts.insert(item);
            minLength = min(minLength, (int)item.length());
            maxLength = max(maxLength, (int)item.length());
        }
        vector<bool> dp(s.size() + 1, false);
        dp[0] = true;
        for (int i = minLength; i <= s.size(); ++i)
        {
            for (int len = minLength; len <= min(maxLength, i); ++len)
            {
                if (dp[i - len] && dicts.count(s.substr(i - len, len)))
                    dp[i] = true;
            }
        }
        return dp[s.size()];
    }
};

note:

  • 这里的最差情况不考虑unordered_set的最差,即认为insert和find都是O(1) time,不会出现hash碰撞

GitHub传送门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值