【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
【139-Word Break(单词拆分)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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".
题目大意
给定一个字符串s和单词字典dict,确定s能否够切割成一个或多个单词空格分隔的序列。
解题思路
一个字符串S,它的长度为N。假设S能够被“字典集合”(dict)中的单词拼接而成,那么所要满足的条件为:
* F(0, N) = F(0, i) && F(i, j) && F(j, N);
* 这样子,假设我们想知道某个子串是否可由Dict中的几个单词拼接而成就能够用这种方式得到结果(满足条件为True, 不满足条件为False)存入到一个boolean数组的相应位置上,这样子,最后boolean 数组的最后一位就是F(0, N)的值,为True表示这个字符串S可由Dict中的单词拼接,否则不行!
代码实现
算法实现类
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
// 參数校验
if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
return false;
}
// 标记是否匹配,match[i]表表[0, i-1]都匹配
int length = s.length();
boolean[] match = new boolean[length + 1];
match[0] = true;
for (int i = 1; i < length + 1; i++) {
for (int j = 0; j < i; j++) {
if (match[j] && wordDict.contains(s.substring(j, i))) {
// f(0,n) = f(0,i) + f(i,j) + f(j,n)
match[i] = true;
break;
}
}
}
return match[length];
}
// 以下是还有一种解法。可是会超时
public boolean wordBreak2(String s, Set<String> wordDict) {
// 參数校验
if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
return false;
}
Map<Character, Set<String>> wordMap = new HashMap<>(wordDict.size());
// 将全部開始字符同样的单词放入一个Set中
for (String word : wordDict) {
Set<String> set = wordMap.get(word.charAt(0));
if (set == null) {
// 新创建一个set放入Map中
set = new HashSet<>();
wordMap.put(word.charAt(0), set);
}
// 单词存入set中
set.add(word);
}
return wordBreak(s, 0, wordMap);
}
/**
* 搜索字符串能否够被切割成单词串
*
* @param s 字符串
* @param idx 处理的開始位置
* @param wordMap 单词字典,開始字符同样的在同一个set集合中
* @return 搜索结果
*/
public boolean wordBreak(String s, int idx, Map<Character, Set<String>> wordMap) {
if (idx >= s.length()) {
return true;
}
Set<String> words = wordMap.get(s.charAt(idx));
if (words != null) {
for (String word : words) {
// idx之前的字符已经匹配,假设从ide之后起匹配word单词
if (s.startsWith(word, idx)) {
// 递归处理
boolean result = wordBreak(s, idx + word.length(), wordMap);
// 假设满足条件,返回true
if (result) {
return true;
}
}
}
}
return false;
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47774547】
【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 139 Word Break 单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...
- [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-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- 【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】
[079-Word Search(单词搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a 2D board and a word, find if ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
随机推荐
- java课堂 笔记
- 【转】tomcat与apache,tomcat与servlet的区别
tomcat与apache的区别:(转自:http://blog.csdn.net/longzs/article/details/10959945) 1.apache支持静态页,tomcat支持动态的 ...
- mysql 连接超时的问题
项目中用mycat做的分表分库,异步通知系统会连接mycat去查数据库数据,有时会抛异常提示mysql server has gone away.最初以为是mycat的问题,在修改了mycat的配置, ...
- 解决报错DEFAULT_INCOMPATIBLE_IMPROVEMENTS
freemarker整合springMVC报错如下:警告: Exception encountered during context initialization - cancelling refre ...
- C/C++注释规范
C/C++注释规范 Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,完全支持C.C++.Java.Objective-C和IDL语言,部分支持PHP.C#.鉴于Doxygen ...
- 挑战程序竞赛例题 4.1 Random Walk(高斯消元求期望值)
给你一幅N*M的地图,地图中有不能到达的障碍物'#'与可以走的点'.',从(1,1)开始走到(N,M),其中每一次走动均等概率地向周围的可达的格子走去,求到达(N,M)的期望步数.(N,M<=1 ...
- So注入工具TsoInject开发文档
So注入工具TsoInject开发文档 导语: 作为一个软件安全从业者而言,我们需要对某个App的关键函数就行Hook, 对于android而言,Smali层我们使用Xposed Hook框架,So层 ...
- 【CCF】路径解析 模拟
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- Bzoj1195 [HNOI2006]最短母串 [状态压缩]
Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1304 Solved: 439 Description 给定n个字符串(S1,S2,„,Sn),要求找 ...
- [水] POJ 3096
Surprising Strings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7659 Accepted: 487 ...