leetcode139 单词拆分

1.解法

1.1 暴力递归

利用递归进行枚举,一个一个试,但是提交会出现超出时间限制

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        return isWord(s,wordDict,0);
    }

    private boolean isWord(String s, List<String> wordDict, int start){
        if (start == s.length()){//1
            return true;
        }

        for (String word : wordDict) {
            int len = word.length();
            int end = start + len;

            //必须保证 添加上新单词后 组成的 单词的 长度 小于 目标单词的长度
            if (end > s.length()){
                continue;
            }

            if (s.substring(start, end).equals(word)){//上面的长度满足后,这里判断单词是否一致
                if (isWord(s, wordDict, end)){//上面单词一致,继续拼接后续单词。递归调用,并作为判断条件,
                                                // 直到最后只有完全正确才会走到上面的1处,并返回true,回到这里,true通过所以返回true,然后再返回,直到最后返回true
                                                // 如果最后发现1处无法通过,并且for循环中也没有能满足长度条件的单词,那只能到2处,然后返回false到此处,发现无法通过,继续到上面拿一个新单词重走for循环。如果最后没有满足,只能走到2处得到false;
                    return true;
                }
            }
        }
        return false;//2
    }
}

时间复杂度 O ( n 2 ) O(n^2) O(n2)


1.2 动态规划

定义一个数组boolean[] dp = new boolean[s.length]dp[3]=true就代表s的索引0~(3-1)处可以由字典里的单词组成。

需要初始化dp[0]=true

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length() + 1];//多一个用来初始化
        dp[0] = true;//初始化

        for (int i = 0; i < s.length(); i++) {
            if (!dp[i]){
                continue;
            }
            for (String word : wordDict) {
                int len = word.length();
                int end = i + len;
                //拼接后长度超过目标单词长度,不符合
                if(end > s.length()){
                    continue;
                }
                //字符串s的i~(end-1)处与word相同,说明拼接正确,记录
                if (s.substring(i, end).equals(word)){
                    dp[end] = true;
                }
            }
        }
        return dp[s.length()];
    }
}

时间复杂度 O ( s . l e n g t h ∗ w o r d D i c t . s i z e ) O(s.length*wordDict.size) O(s.lengthwordDict.size)


1.3 正则表达式

s = "leetcode", wordDict = ["leet", "code"]为例,将字典中的所有单词拼接成(leet|code)*,再去做匹配,其中()标记一个子表达式的开始和结束位置,|指明两项之间的一个选择,*匹配前面的子表达式零次或多次。

正则表达式可以参考菜鸟教程

import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        String re = "(";
        for(int i = 0; i < wordDict.size(); i++){
            re += wordDict.get(i) + "|";
        }
        re += ")*";
        System.out.println(re);
        Pattern p = Pattern.compile(re);
        Matcher m = p.matcher(s);
        return m.matches();
    }
}

leetcode中需要手动引入PatternMatcher。感觉使用这种方法做这道题,这道题也就没有意义了,了解一下


2.原题

给你一个字符串 s 和一个字符串列表 wordDict 作为字典。如果可以利用字典中出现的一个或多个单词拼接出 s 则返回 true

**注意:**不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。

示例 1:

输入: s = "leetcode", wordDict = ["leet", "code"]
输出: true
解释: 返回 true 因为 "leetcode" 可以由 "leet" 和 "code" 拼接成。

示例 2:

输入: s = "applepenapple", wordDict = ["apple", "pen"]
输出: true
解释: 返回 true 因为 "applepenapple" 可以由 "apple" "pen" "apple" 拼接成。
     注意,你可以重复使用字典中的单词。

示例 3:

输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
输出: false

提示:

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • swordDict[i] 仅由小写英文字母组成
  • wordDict 中的所有字符串 互不相同
  • 62
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值