Word Break II——LeetCode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
题目大意就是:给一个string,一个词典,把这个string根据词典构建出所有可能的组合。
我的解法就是预处理+DFS+剪枝。
1、首先用一个breakFlag数组,记录string中的各个位置为结尾是否是合法的分词末尾,以上面的样例来说,c,a都是不合法的分词结尾,t,s都是合法的分词结尾。
2、然后用DFS来搜全部可能的组合,如果最后正好分完这个string,说明是合法的分词方法,组合成句子然后添加到结果List中,有个小优化就是可以先把字典里的单词的长度放入一个array,分词的时候只需要遍历这个array就可以,比如上面的字典里单词长度只有{3,4}组成一个array,只需要遍历cat cats就可以继续往后遍历了。
Talk is cheap>>
package leetcode; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; public class WordBreakII { public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("cat");
set.add("cats");
set.add("and");
set.add("sand");
set.add("dog");
new WordBreakII().wordBreak("catsanddog", set);
} Set<Integer> lenArray = new HashSet<>();
boolean[] breakFlag; public List<String> wordBreak(String s, Set<String> dict) {
List<String> res = new ArrayList<>();
for (String next : dict) {
lenArray.add(next.length());
}
breakFlag = new boolean[s.length() + 1];
breakFlag[0] = true;
for (int i = 0; i < s.length(); i++) {
if (breakFlag[i]) {
for (int j = 0; i + j < s.length() + 1; j++) {
if (dict.contains(s.substring(i, i + j)))
breakFlag[i + j] = true;
}
}
}
if (breakFlag[s.length()])
dfs(s, "", dict, res, s.length());
return res;
} public void dfs(String src, String tmp, Set<String> dict, List<String> res, int length) {
if (length < 0) {
return;
}
if (length == 0) {
System.out.println(tmp.substring(0, tmp.length() - 1));
res.add(tmp.substring(0, tmp.length() - 1));
return;
}
for (int len : lenArray) {
int left = src.length() - len;
if (left < 0)
break;
String t = src.substring(left, src.length());
if (breakFlag[length] && dict.contains(t)) {
dfs(src.substring(0, left), t + " " + tmp, dict, res, length - len);
}
}
} }
Word Break II——LeetCode的更多相关文章
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- Word Break II -- leetcode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【LeetCode】140. Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
随机推荐
- Meth | elementary OS常用配置
一,搜狗输入法 sudo apt-get remove ibussudo add-apt-repository ppa:fcitx-team/nightlysudo apt-get updatesud ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- Sublime Text3使用详解
Sublime Text简介 Sublime Text - 性感的代码编辑器.程序员之必备神器 Sublime Text 是一个代码编辑器,也是HTML和散文先进的文本编辑器.Sublime Text ...
- 昨天做了一个使用javamail发送文件的demo
记录一下过程. 两种版本第一个demo是纯java文件.一个就可以,是我在网上搜索到的,第二个demo是我在ssh框中中写的jsp页面demo 1.java版本: package com.zq.www ...
- 【javascript模式】Chapter2: 基本 技巧
1 尽量少用全局变量,最好一个应用程式只有一个全局变量 隐含全局变量(不使用var声明)与明确定义的全局变量区别: (1)使用var创建的全局变量(在函数外部声明)不能用delete删除 (2) ...
- scn转换为十进制
- xmpp发送文件
xmpp 文件传输协议: XEP-0096: SI File Transfer:文件传输流初始化协议 XEP-0065: SOCKS5 Bytestreams:带外socks5代理字节流传输协议 XE ...
- 虚拟器运行iOS8地图提示错误
/SourceCache/ProtocolBuffer_Sim/ProtocolBuffer-225/Runtime/PBRequester.m:799 server (https://gsp13-c ...
- 加入BOINC(伯克利开放式网络计算平台)
转载:BOINC:为科学而计算 通过 BOINC 你可以将闲置的计算机时间贡献给 SETI@home, Climateprediction.net, Rosetta@home, World Commu ...
- phpcms v9自定义表单提交后返回上一页实现方法
PHPcms v9中提交自定义表单后默认都是回到首页的,是不是感觉很不爽! 接下来,就说下phpcms v9自定义表单提交后返回上一页实现方法. 1.找到这个文件 phpcms\modules\for ...