给定一个字符串 String s =
"leetcode"


dict =
["leet", "code"]
.

查看一下是够是字典中的词语组成。假设是返回true,否则返回false。

下边提供3种思路

1.动态算法

import java.util.HashSet;
import java.util.Set; public class WordBreak1 {
public static void main(String[] args) {
//"["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
//String s="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
String s ="LeetCodea";
Set<String> dict = new HashSet<String>();
dict.add("Leet");
dict.add("Code");
dict.add("a");
System.out.println(wordBreak(s,dict));
}
public static boolean wordBreak(String s, Set<String> dict) {
boolean[] t = new boolean[s.length() + 1];
t[0] = true; // set first to be true, why?
// Because we need initial state for (int i = 0; i < s.length(); i++) {
// should continue from match position
if (!t[i])
continue;
for (String a : dict) {
int len = a.length();
int end = i + len;
if (end > s.length())
continue; if (t[end])
continue; if (s.substring(i, end).equals(a)) {
t[end] = true;
}
}
}
return t[s.length()];
}
}

2.普通算法(1)

import java.util.Set;

public class WorkBreak2 {
public boolean wordBreak(String s, Set<String> dict) {
return wordBreakHelper(s, dict, 0);
} public boolean wordBreakHelper(String s, Set<String> dict, int start) {
if (start == s.length())
return true; for (String a : dict) {
int len = a.length();
int end = start + len; // end index should be <= string length
if (end > s.length())
continue; if (s.substring(start, start + len).equals(a))
if (wordBreakHelper(s, dict, start + len))
return true;
}
return false;
}
}

3.普通算法(2)

import java.util.Set;

public class WordBreak3 {

	public static boolean wordBreak(String s, Set<String> dict) {
// input validation
// Base case
if (dict.contains(s))
return true;
else {
for (int i = 0; i < s.length(); i++) {
String sstr = s.substring(0, i);
if (dict.contains(sstr))
return wordBreak(s.substring(i), dict);
}
}
return false;
}
}

可是以上的算法有一个问题,就是遇到这样的情况。INPUT: "programcreek", ["programcree","program","creek"]. 无能为力。

大家讨论下吧?

Java Word Break(单词拆解)的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

  3. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  4. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  5. Leetcode139. Word Break单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...

  6. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  7. Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode

    这道题相似  Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...

  8. LeetCode 139. 单词拆分(Word Break)

    139. 单词拆分 139. Word Break

  9. word break II(单词切分)

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

随机推荐

  1. Appium + python - long_press定位操作实例

    from appium.webdriver.common.touch_action import TouchActionfrom appium import webdriverimport timei ...

  2. C - Tram

    Problem description Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n i ...

  3. Halcon学习笔记之支持向量机(二)

    例程:classify_halogen_bulbs.hdev 在Halcon中模式匹配最成熟最常用的方式该署支持向量机了,在本例程中展示了使用支持向量机对卤素灯的质量检测方法.通过这个案例,相信大家可 ...

  4. 树莓派-USB存储设备自动挂载

    简单介绍实现命令行下USB存储设备自动挂载的方法,Linux gnome/kde窗口环境下有移动存储的管理程序,可以实现自动挂载移动存储设备,但是在命令行下 通常需要用mount命令手动挂载USB存储 ...

  5. js,jquery中.each()方法遍历如何终止循环

    用.each()方法遍历节点的时候,用“return false”只能终止当前循环并跳入下一次循环,并不能终止所有循环.代码如下: $(".days").each(function ...

  6. ie8及其以下版本兼容性问题之input file隐藏上传文件

    文件上传时,默认的file标签很难看,而且每个浏览器下都有很大差距.因此我们基本都把真正的file标签给隐藏,然后创建一个标签来替代它.但是由于IE出于安全方面的考虑上传文件时必须点击file的浏览按 ...

  7. js-常见简单的js判断方法(暂不参考正则)

    1: 2: 3: 4: 5: 6: 7:

  8. 解决无法移除tomcat中的项目

    问题:启动myeclipse,tomcat提示报错,blind,但是你移除的时候无法移除,只会显示一个黄色的感叹号,此时你直接在webapp中删除时,也提示呗占用无法删除. 办法:关掉myeclips ...

  9. Eclipse中配置SVN(步骤简述)

    ————Eclipse中配置SVN(步骤简述)———— 1.有客户端(tortoiseSVN),服务器端(visualSVN) 两种,根据需要安装,安装后需重启电脑 2.服务器端配置:创建版本库(放工 ...

  10. 【Bootstrap】如何让响应式图片(img-responsive)水平居中

    我们在用bootstrap排版内容的时候,有的时候在内容中需要图片水平居中对齐. 一般情况下,我们的图片都使用了 .img-responsive 类来实现响应式图片.如果需要实现响应式图片水平居中,那 ...