Word Break 判断是否能把字符串拆分为字典里的单词 @LeetCode
2013-12-23 07:01
323 查看
思路:
1 DFS 但是TLE
2 DP 还需多练。。。
http://www.programcreek.com/2012/12/leetcode-solution-word-break/
http://xixiaogualu.blogspot.com/2013/10/leetcode-word-break.html
package Level5;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
/**
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 S147 {
public static void main(String[] args) {
Set<String> dict = new HashSet<String>();
dict.add("aaa");
dict.add("aaaa");
String s = "aaaaaaa";
System.out.println(wordBreak(s, dict));
System.out.println(wordBreak2(s, dict));
}
// DP
public static boolean wordBreak(String s, Set<String> dict) {
// 如果canBreak[i]为true,则s[0...(i-1)]能被拆分
boolean[] canBreak = new boolean[s.length()+10];
canBreak[0] = true; // 初始化
for(int i=0; i<s.length(); i++){
if(canBreak[i] == false){
continue;
}
for(String dictS : dict){
int len = dictS.length();
int end = i + len;
if(end > s.length()){
continue;
}
if(s.substring(i, end).equals(dictS)){
canBreak[end] = true;
}
}
}
return canBreak[s.length()];
}
// DFS TLE
public static boolean wordBreak2(String s, Set<String> dict) {
return dfs(s, dict, 0);
}
public static boolean dfs(String s, Set<String> dict, int start){
if(start >= s.length()){
return true;
}
for(String dictS : dict){
int len = dictS.length();
if(start+len<=s.length() && s.substring(start, start+len).equals(dictS)){
if(dfs(s, dict, start+len)){
return true;
}
}
}
return false;
}
}
重写了一遍,逻辑更加清晰:
核心思想是:
如果一个字符串 s[0,i) 能被拆分,则一定能找到一个j使得:s[0,j) 能被拆分 且 s[j,i) 在字典中
注意字符串区间是左闭右开!
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
if(s.length() == 0) {
return false;
}
boolean[] canBreak = new boolean[s.length()+1]; // canBreak[i]: whether s[0,i) can be break
canBreak[0] = true; // base case
for(int i=1; i<=s.length(); i++) { // last case: s[0,s.length()) == s
boolean flag = false;
for(int j=0; j<i; j++) { // j should be smaller than i to include at least character
// s[0,i) can be break only when s[0,j) can be break AND s[j,i) is in the dict
if(canBreak[j] && dict.contains(s.substring(j, i))) {
flag = true;
break;
}
}
canBreak[i] = flag;
}
return canBreak[s.length()];
}
}
1 DFS 但是TLE
2 DP 还需多练。。。
http://www.programcreek.com/2012/12/leetcode-solution-word-break/
http://xixiaogualu.blogspot.com/2013/10/leetcode-word-break.html
package Level5;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
/**
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 S147 {
public static void main(String[] args) {
Set<String> dict = new HashSet<String>();
dict.add("aaa");
dict.add("aaaa");
String s = "aaaaaaa";
System.out.println(wordBreak(s, dict));
System.out.println(wordBreak2(s, dict));
}
// DP
public static boolean wordBreak(String s, Set<String> dict) {
// 如果canBreak[i]为true,则s[0...(i-1)]能被拆分
boolean[] canBreak = new boolean[s.length()+10];
canBreak[0] = true; // 初始化
for(int i=0; i<s.length(); i++){
if(canBreak[i] == false){
continue;
}
for(String dictS : dict){
int len = dictS.length();
int end = i + len;
if(end > s.length()){
continue;
}
if(s.substring(i, end).equals(dictS)){
canBreak[end] = true;
}
}
}
return canBreak[s.length()];
}
// DFS TLE
public static boolean wordBreak2(String s, Set<String> dict) {
return dfs(s, dict, 0);
}
public static boolean dfs(String s, Set<String> dict, int start){
if(start >= s.length()){
return true;
}
for(String dictS : dict){
int len = dictS.length();
if(start+len<=s.length() && s.substring(start, start+len).equals(dictS)){
if(dfs(s, dict, start+len)){
return true;
}
}
}
return false;
}
}
重写了一遍,逻辑更加清晰:
核心思想是:
如果一个字符串 s[0,i) 能被拆分,则一定能找到一个j使得:s[0,j) 能被拆分 且 s[j,i) 在字典中
注意字符串区间是左闭右开!
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
if(s.length() == 0) {
return false;
}
boolean[] canBreak = new boolean[s.length()+1]; // canBreak[i]: whether s[0,i) can be break
canBreak[0] = true; // base case
for(int i=1; i<=s.length(); i++) { // last case: s[0,s.length()) == s
boolean flag = false;
for(int j=0; j<i; j++) { // j should be smaller than i to include at least character
// s[0,i) can be break only when s[0,j) can be break AND s[j,i) is in the dict
if(canBreak[j] && dict.contains(s.substring(j, i))) {
flag = true;
break;
}
}
canBreak[i] = flag;
}
return canBreak[s.length()];
}
}
相关文章推荐
- Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode
- 判断字符串是否能分割成字典中的单词(二)——Leetcode系列(十二)
- 判断字符串是否能分割成字典中的单词(一)——Leetcode系列(十一)
- Word Break II 求把字符串拆分为字典里的单词的所有方案 @LeetCode
- 切词分词之判断字符串是否能够拆分为字段中的单词
- 【Manthan, Codefest 16C】【DP SET-MAP 字典树哈希法】Spy Syndrome 2 字符串是否由字典库单词反转加密而成
- 二叉搜索树应用-判断一个单词是否拼写正确,实现简单字典
- 判断字符串中是否包含某个单词
- 给定一个字符串s和一个单词dict的字典,确定s是否可以分割成一个或多个字典单词的空格分隔的序列。 s ="leetcode", dict =["leet", "code"].
- Java 写一个方法判断一个字符串是否对称 "asdfgasdfg"、编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
- oracle中使用split拆分字符串,并且判断字符串是否在数据库中存在
- hdu1247map+string判断是否子单词出现在字典中
- 字符串单词拆分 Word Break
- 哈希表对字符串的高效处理6:判断两个单词是否为变位词
- 给定一个源字符串,一个字符串字典,判断该字典是否是由源字符串完全分割而来的
- 编程判断字符串是否为回文 判断一个字符串是否是回文,例如单词‘level’
- Oracle判断指定字符串拆分后是否包含另一个字符换
- python判断变量是否为int、字符串、列表、元组、字典等方法
- 判断是否为素数和统计字符串中单词的个数
- C# 判断字符串是否是22位数字