139. 单词拆分

题目:

139. 单词拆分
在这里插入图片描述

题解:

  1. 解释一:
    在这里插入图片描述
  2. 解释二:
    在这里插入图片描述
/*
    动态规划算法,dp[i]表示s前i个字符能否拆分
    转移方程:dp[j] = dp[i] && check(s[i+1, j]);
    check(s[i+1, j])就是判断i+1到j这一段字符是否能够拆分
    其实,调整遍历顺序,这等价于s[i+1, j]是否是wordDict中的元素
    这个举个例子就很容易理解。
    假如wordDict=["apple", "pen", "code"],s = "applepencode";
    dp[8] = dp[5] + check("pen")
    翻译一下:前八位能否拆分取决于前五位能否拆分,加上五到八位是否属于字典
    (注意:i的顺序是从j-1 -> 0哦~)
*/

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

代码:

1. 常规代码:

import java.util.*;

public class code139 {

    // 常规代码:
    public static boolean wordBreak(String s, List<String> wordDict) {
        Set<String> wordDictSet = new HashSet<>(wordDict);
        // dp[i] 表示字符串 s 前 i 个字符组成的字符串 s[0..i-1] 是否能被空格拆分成若干个字典中出现的单词
        boolean dp[] = new boolean[s.length() + 1];
        dp[0] = true; // dp[0] = true 表示空串且合法
        for(int i = 1; i <= s.length(); i++)
        {
            for(int j = 0; j < i; j++)
            {
                if(dp[j] && wordDictSet.contains(s.substring(j, i)))
                {
                    dp[i] = true;
                    break; // break退出的是内层的for循环
                }
            }
        }
        return dp[s.length()];
    }

    public static void main(String[] args) {
        String s1 = "leetcode";
        List<String> wordDict1 = new ArrayList<String>();
        wordDict1.add("leet");
        wordDict1.add("code");
        boolean res1 = wordBreak(s1, wordDict1);
        System.out.println(res1);

        String s2 = "applepenapple";
        List<String> wordDict2 = new ArrayList<String>();
        wordDict2.add("apple");
        wordDict2.add("pen");
        boolean res2 = wordBreak(s2, wordDict2);
        System.out.println(res2);

        String s3 = "catsandog";
        List<String> wordDict3 = new ArrayList<String>();
        wordDict3.add("cats");
        wordDict3.add("dog");
        wordDict3.add("sand");
        wordDict3.add("and");
        wordDict3.add("cat");
        boolean res3 = wordBreak(s3, wordDict3);
        System.out.println(res3);
    }
}

2. 优化代码:

import java.util.*;

public class code139 {

    // 优化代码:
    public static boolean wordBreak(String s, List<String> wordDict) {
        int minLen = Integer.MAX_VALUE;
        int maxLen = 0;
        for(int i = 0; i < wordDict.size(); i++)
        {
            if(wordDict.get(i).length() < minLen)
            {
                minLen = wordDict.get(i).length(); // 获取最小单词长度
            }
            if(wordDict.get(i).length() > maxLen)
            {
                maxLen = wordDict.get(i).length(); // 获取最大单词长度
            }
        }

        Set<String> wordDictSet = new HashSet<>(wordDict);
        // dp[i] 表示字符串 s 前 i 个字符组成的字符串 s[0..i-1] 是否能被空格拆分成若干个字典中出现的单词
        boolean dp[] = new boolean[s.length() + 1];
        dp[0] = true; // dp[0] = true 表示空串且合法
        for(int i = minLen; i <= s.length(); i++) // i不用从1开始,只要从wordDict里面字符串(单词)的最小长度开始就行
        {
            for(int j = Math.max(0, i - maxLen); j <= i - minLen; j++) // 每次并不需要从s[0]开始搜索。因为wordDict中的字符串长度是有限的。只需要从i-maxWordLength开始搜索就可以了。
            {
                if(dp[j] && wordDictSet.contains(s.substring(j, i)))
                {
                    dp[i] = true;
                    break; // break退出的是内层的for循环
                }
            }
        }
        return dp[s.length()];
    }

    public static void main(String[] args) {
        String s1 = "leetcode";
        List<String> wordDict1 = new ArrayList<String>();
        wordDict1.add("leet");
        wordDict1.add("code");
        boolean res1 = wordBreak(s1, wordDict1);
        System.out.println(res1);

        String s2 = "applepenapple";
        List<String> wordDict2 = new ArrayList<String>();
        wordDict2.add("apple");
        wordDict2.add("pen");
        boolean res2 = wordBreak(s2, wordDict2);
        System.out.println(res2);

        String s3 = "catsandog";
        List<String> wordDict3 = new ArrayList<String>();
        wordDict3.add("cats");
        wordDict3.add("dog");
        wordDict3.add("sand");
        wordDict3.add("and");
        wordDict3.add("cat");
        boolean res3 = wordBreak(s3, wordDict3);
        System.out.println(res3);
    }
}

参考:

  1. 单词拆分
  2. 【手绘图解】三种方法及其优化:DFS、BFS、动态规划
  3. 【单词拆分】拒绝装x,从简单的想法出发,轻松识破动态规划小套路
  4. 两层FOR循环中,调用break,退出的是哪一层的循环?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dev_zyx

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

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

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

打赏作者

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

抵扣说明:

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

余额充值