【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 ...
随机推荐
- maven学习(六)——在别的项目中引用通过Maven安装生成的项目的jar包
1.新建HelloFriend项目,同时建立Maven约定的目录结构和pom.xml文件 HelloFriend | --src | -----main | ----------java | - ...
- CH Round #59 - OrzCC杯NOIP模拟赛day1
第一题:队爷的新书 题意简述:给定n个闭区间,求出一个数p使它与包含它的区间数的积最大,输出这个积. 分析:使用一个差分数组g,每个区间[l,r],l位置加1,r+1的位置减1,从前往后统计,得到对于 ...
- codechef May Challenge 2016 CHSC: Che and ig Soccer dfs处理
Description All submissions for this problem are available. Read problems statements in Mandarin Chi ...
- 分布式文件系统之Glusterfs
1.环境规划如下 centos7.4 三个节点一块 sdb 3G大小的测试硬盘 2.Glusterfs 卷的类型比较多,这里我们测试最常用的一种 Distributed Replicate ...
- Python之文件操作:文件、目录的操作
一.创建 1.创建文件 open(path,'w') 2.创建目录 (1)os.mkdir(pt[, mode=0777]) 新建一个目录pt,参数mode表示生成的目录的权限,默认是超级权限,也就是 ...
- Codevs 1315 摆花
1315 摆花 2012年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 小明的花店新开张,为了吸引顾客,他 ...
- sql语句like的用法 有些正则表达式可以通过like实现
原文发布时间为:2010-10-28 -- 来源于本人的百度文章 [由搬家工具导入] 在SQL结构化查询语言中,LIKE语句有着至关重要的作用。LIKE语句的语法格式是:select * from 表 ...
- 配置微信api调扫码功能
var url = encodeURIComponent(location.href.split('#')[0]); $.get(iapi+'/htweb/wx/getJsSdkSign?url='+ ...
- [LeetCode] Unique Binary Search Trees II dfs 深度搜索
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Codeforces Round #454 Div. 2 A B C (暂时)
A. Masha and bears 题意 人的体积为\(V\),车的大小为\(size\),人能钻进车的条件是\(V\leq size\),人对车满意的条件是\(2V\geq size\). 现知道 ...