import java.util.Arrays;
import java.util.HashSet;
import java.util.Set; /**
* Source : https://oj.leetcode.com/problems/word-break/
*
*
* Given a string s and a dictionary of words dict, determine if s can be segmented
* into a space-separated sequence of one or more dictionary words.
*
* For example, given
* s = "leetcode",
* dict = ["leet", "code"].
*
* Return true because "leetcode" can be segmented as "leet code".
*
*/
public class WordBreak { /**
* 判断给定的字符串是否能分割为多个单词,每个单词都包含在给定的字典中
*
* 1. 使用DFS,如果能到达字符串最后,说明可以break
* 2. 题目中只是判断是否的问题,并不需要求出具体break的方法,对于是否的问题可以使用DP来解决
*
* dp[i]:表示str[i-1]能否被break,比如dp[1]表示str[0:0]能否被break
* dp[0] = true
* dp[i] = true,当:
* 存在0 <= k <= i-1, dp[k] = true, 并且tr[k:i-1] 存在dic中
*
* @param str
* @param dic
* @return
*/
public boolean wordBreak (String str, Set<String> dic) {
boolean[] dp = new boolean[str.length()+1];
dp[0] = true;
for (int i = 0; i < str.length(); i++) {
for (int j = i; j > -1; j--) {
if (dp[j] && dic.contains(str.substring(j, i+1))) {
dp[i + 1] = true;
break;
}
}
}
return dp[str.length()];
} public static void main(String[] args) {
WordBreak wordBreak = new WordBreak();
String[] dicStr = new String[]{"leet", "code"};
boolean result = wordBreak.wordBreak("leetcode", new HashSet<String>(Arrays.asList(dicStr)));
System.out.println(result + " == true");
}
}

leetcode — word-break的更多相关文章

  1. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  2. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  3. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  4. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  5. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  6. LeetCode ||& Word Break && Word Break II(转)——动态规划

    一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...

  7. [LeetCode] Word Break II 解题思路

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. [Leetcode] word break ii拆分词语

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. LeetCode: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

  10. [LeetCode] Word Break 拆分词句

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. SQL Server学习之路(二):主键和外键

    0.目录 1.定义 1.1 什么是主键和外键 1.2 主键和外键的作用 1.3 主键.外键和索引的区别 2.主键(primary key) 2.1 通过SSMS设置主键 2.2 通过SQL语句设置主键 ...

  2. linux shell中单引号、双引号和没有引号的区别

    单引号: 可以说是所见即所得:即将单引号的内的内容原样输出,或者描述为单引号里面看到的是什么就会输出什么. 双引号: 把双引号内的内容输出出来:如果内容中有命令.变量等,会先把变量.命令解析出结果,然 ...

  3. 小白的Python之路 day1 变量

    Python之路,Day1 - Python基础1 变量 变量用于存储在计算机程序中引用和操作的信息.它们还提供了一种用描述性名称标记数据的方法,这样我们的程序就能更清晰地被读者和我们自己理解.将变量 ...

  4. 工作中git 操作汇总

    1. git branch -l  查看本地branch 2. git reset --hard 回滚全部修改 3. git status  查看本地修改 4. git pull 更新代码 5. gi ...

  5. JAVA入门[2]-安装Maven

    一.资料 1.官网: https://maven.apache.org/ 二.下载Maven 下载地址:https://maven.apache.org/download.cgi# 三.Windows ...

  6. 秦俊:开放 DevOps 敏捷开发套件,助力开发者驰骋云端

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ DevOps可以让人工智能(AI).大数据(Bigdata).云计算(Cloud)更加高效地落地,越来越多的企业和团队在践行DevOps. ...

  7. ME01:猎场中猎头的内核

    前几天追了下<猎场>,只看了前面10多集,觉得下面的对话有点意思. 是关于猎头是干什么的? 猎头具备的素质. 对我们普通人是不是也有启发意义呢? 如何看人, 找到靠谱的合作人?找打好的老板 ...

  8. java实现播放mp3功能

    1.首先引入jlayer.jar <!-- https://mvnrepository.com/artifact/javazoom/jlayer --> <dependency> ...

  9. 自学Zabbix3.9.3-模板Templates-嵌套Nesting

    自学Zabbix3.9.3-模板Templates-嵌套Nesting 嵌套是一个模板包含一个或多个其他模板的方法.可以在一个"嵌套"模板中将一些模板链接在一起.嵌套的好处在于,只 ...

  10. 什么是 stack?- 每天5分钟玩转 Docker 容器技术(111)

    什么是 stack ?在回答这个问题之前我们先回忆一下前面部署 WordPress 应用的过程: 首先创建 secret. 然后创建 MySQL service,这是 WordPress 依赖的服务. ...