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. Java数据结构和算法(八)——递归

    记得小时候经常讲的一个故事:从前有座山,山上有座庙,庙里有一个老和尚和一个小和尚,一天,老和尚给小和尚讲了一个故事,故事内容是“从前有座山,山上有座庙,庙里有一个老和尚和一个小和尚,一天,老和尚给小和 ...

  2. JMeter接口测试系列-关联参数

    这里主要记录一下A接口的返回结果经过md5加密之后作为另外B接口的参数,这个问题困扰了很久,找了不少资料,现在把解决方法记录如下: 环境 ①JMeter 3.0 ②前置条件:将fastjson.jar ...

  3. vue.js介绍,常用指令,事件,以及制作简易留言版

    一.vue是什么? 一个mvvm框架(库).和angular类似,比较容易上手.小巧,让我们的代码更加专注于业务逻辑,而不是去关注DOM操作 二.vue和angular之间的区别 vue--简单易学 ...

  4. 一个看起来不像中年人的中年人,带着两个初出茅庐的小伙子儿,用git管理项目代码的进击之路

    一个中年人的孤独前行 我们这一代人,是上个世纪的人,活在当下,已然成为社会上的中流砥柱. 80年代生人,遥望我们的父辈,均是5.60年代的人,迟迟暮年,夕夕老矣.而我们,正当年,却又时光飞逝,很快便要 ...

  5. c++ 求集合的交并补

    #include<iostream.h> #include<windows.h> #include<iomanip.h> #include<stdio.h&g ...

  6. Python 日期和时间操作

    Python提供了一个time 和calendar模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都是以自从1970年1月1日午夜(历元)经过了多长时间来表示. Pytho ...

  7. gRPC异步处理应答

    gRPC异步处理应答 (金庆的专栏) gRPC的演示样例 greeter_async_client.cc 不算是异步客户端,它使用了异步请求.可是堵塞式等待应答,结果成为一个同步调用. std::st ...

  8. C++教程之autokeyword的使用

    一.autokeyword的前世 从C语言開始,autokeyword就被当作是一个变量的存储类型修饰符,表示自己主动变量(局部变量).它不能被单独使用,否则编译器会给出警告. #include &l ...

  9. 项目实战13—企业级虚拟化Virtualization-KVM技术

    项目实战系列,总架构图 http://www.cnblogs.com/along21/p/8000812.html KVM的介绍.准备工作和qemu-kvm 命令详解 1.介绍 (1)介绍 KVM:就 ...

  10. 结对编程-四则运算GUI的实现

    一.项目成员以及coding地址: 洪灏捷(本人)201321122020  coding地址:https://git.coding.net/hoje/The-GUI-operation.git 白至 ...