#139 Word Break

Description

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.

Examples

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

Constraints:

1 <= s.length <= 300
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 20
s and wordDict[i] consist of only lowercase English letters.
All the strings of wordDict are unique.

思路

  • 最开始的时候我理解错了题目意思,我以为要用到wordDict中的所有单词才算true,但其实是只要可以通过wordDict中的一个或几个单词组成s就可以达成true
  • 看到题目的时候我想到的就是递归,下面递归的代码有restWords和backUpWords就是因为最开始的理解错误,后面发现了错误只改了退出条件没有改中间流程,就保留下来了。
    • 具体的步骤就是依次判断当前字符串能不能由restWords+backUpWords中的单词组成

  • 但是递归算法会TLE,秉承着所有递归都能写成循环的理论,我想到了dp的方法,构建dp数组,这个数组的长度就是s的长度,数组为boolean类型,每个boolean表示能否用wordDict中的单词组成s.substring(0, current_position)
    • 状态转移方程:当前位置能否为 true 取决于 wordDict 中是否存在一个长度为length的word,使得 dp[current_position - length] = true 且 s.substring(current_position - length, current_position) = word

代码

递归代码

class Solution {
    public boolean canBreak(String s, List<String> restWords, List<String> backUpWords){
        if (s.length() == 0)
            return true;
        
        for (String word: restWords){
            if (s.indexOf(word) == 0){
                List<String> newRestWords = new ArrayList<>(restWords);
                List<String> newBackUpWords = new ArrayList<>(backUpWords);
                newRestWords.remove(word);
                newBackUpWords.add(word);
                if (canBreak(s.substring(word.length()), newRestWords, newBackUpWords))
                    return true;
            }
        }
        
        for (String word: backUpWords){
            if (s.indexOf(word) == 0){
                if (canBreak(s.substring(word.length()), restWords, backUpWords))
                    return true;
            }
        }
        
        return false;
        
    }
    public boolean wordBreak(String s, List<String> wordDict) {
        return canBreak(s, wordDict, new ArrayList<>());
    }
}

DP代码

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Boolean[] flag = new Boolean[s.length() + 1];
        flag[0] = true;
        for (int i = 1; i < s.length() + 1; i++)
            flag[i] = false;
        for (int i = 1; i < s.length() + 1; i++){
            for (String word: wordDict){
                if (i - word.length() >= 0 && s.substring(i - word.length()).indexOf(word) == 0 && flag[i - word.length()]){
                    flag[i] = true;
                    break;
                }
            }
        }
        
        return flag[s.length()];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值