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的更多相关文章

  1. 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 ...

  2. Word Break II -- leetcode

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  3. 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 ...

  4. 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 ...

  5. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  6. 【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 ...

  7. 【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 ...

  8. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  9. 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 ...

随机推荐

  1. Fedora14下首次搭建Samba服务器遇到的一些问题

    SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的通信协议.而Samba则是在Linux和Unix系统上实现SMB协议的一个免费软件,由服务器及客户端程 ...

  2. Android -- 官方下拉刷新SwipeRefreshLayout

    V4的兼容包 API 大概就这4个常用的方法. code 布局 <RelativeLayout xmlns:android="http://schemas.android.com/ap ...

  3. gridview添加header

    gridview是不能添加header的,这里的解决方法是将listview改造成gridview使用,功能很好用,唯一的缺点是列数不能自适应 示例代码下载地址http://pan.baidu.com ...

  4. ubuntu 连接 mssql <转>

    转自  http://www.sendong.net/thread-90941-1-1.html 在linux下连接MSSQL,因为微软同志没有提供任何接口给开发人员,大约他们认为要用MSSQL的,只 ...

  5. (转)java之多线程

    Java线程:概念与原理 一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程 ...

  6. node.js常用的几个模块总结

    /** 一 util *      是 node 里面一个工具模块 ,node 里面几乎所有的模块 都会用到 在这个模块 *  功能: *      1 实现继承 这是主要功能 *      2 实现 ...

  7. Notification封装(没做从网络下载)

    1.Notification作为消息通知,这里简单封装了使用 建立一个bean package com.jcut.view; /** * Author:JsonLu * DateTime:2016/2 ...

  8. Ext.grid.Panel表格分页

    转载:http://www.cnblogs.com/libingql/archive/2012/04/22/2464994.html cshtml @{ Layout = null; } <!D ...

  9. expdp 备份数据库-附带报错信息

    操作系统层面创建目录 [root@Oracle11g ~]# mkdir -p /home/oracle/db_back/ 修改目录的所属用户.所属组 [root@Oracle11g ~]# chow ...

  10. 保垒机SSH登录脚本

    线上服务器一般都会有一个保垒机,我们登录线上服务器的时候都要通过这个堡垒机来实现登录,这样我们就要维护很多线上服务器的ip,很麻烦. 所以写了一个脚本用来便捷的登录各个服务器,可以把这个脚本放到跳板机 ...