Java Word Break(单词拆解)
给定一个字符串 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(单词拆解)的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- 139 Word Break 单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Leetcode139. Word Break单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...
- [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 ...
- Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode
这道题相似 Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...
- LeetCode 139. 单词拆分(Word Break)
139. 单词拆分 139. Word Break
- word break II(单词切分)
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
随机推荐
- 关于TJOI2014的一道题——Alice and Bob
B Alice and Bob •输入输出文件: alice.in/alice.out •源文件名: alice.cpp/alice.c/alice.pas • 时间限制: 1s 内存限制: 128M ...
- File入门及路径名问题
package com.io.file; import java.io.File; /** * @author 王恒 * @datetime 2017年4月20日 下午2:53:29 * @descr ...
- Elasticsearch之批量操作bulk
1.bulk相当于数据库里的bash操作. 2.引入批量操作bulk,提高工作效率,你想啊,一批一批添加与一条一条添加,谁快? 3.bulk API可以帮助我们同时执行多个请求 4.bulk的格式: ...
- Vue页面间传值,以及客户端数据存储
初学Vue,遇到了页面传值的问题,大概网上学习了解了一下,在此跟大家分享一下学习心得,欢迎批评指正. 一.参数传值 如果是简单的页面传值,比如传一个id到详情页等等,推荐使用参数传值. 这里页面是通过 ...
- 浅谈ByteBuffer转换成byte[]时遇到的问题
有些时候我们要把ByteBuffer转换成byte[]来使用.于是很多时候会用以下代码来转换: ByteBuffer buf; .....(一些往buffer写数据的操作) byte[] bs= ne ...
- svn命令行批量删除和批量添加
svn命令行批量删除和批量添加 如果使用svn的命令行,例如在linux下的终端中使用,svn的添加命令是svn add,删除命令是svn del,但是缺乏批量的操作,如果我在资源管理器中,手动添加了 ...
- python3设置打开文件的编码
f = open(file_path,'r',encoding='utf8') 用起来很方便,不需要先读取再转码了.
- (转)Arcgis for JS实现台风运动路径与影像范围的显示
http://blog.csdn.net/gisshixisheng/article/details/42025435 首先,看看具体的效果: 初始化状态 绘制中 绘制完成 首先,组织数据.我组织的数 ...
- Java设计模式之JDK动态代理原理
动态代理核心源码实现public Object getProxy() { //jdk 动态代理的使用方式 return Proxy.newProxyInstance( this.getClass(). ...
- PAT_A1034#Head of a Gang
Source: PAT A1034 Head of a Gang (30 分) Description: One way that the police finds the head of a gan ...