【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(单词拆分)】的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  3. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

  4. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  5. Leetcode139. Word Break单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...

  6. 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

    [058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...

  7. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

    [030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...

  8. 【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】

    [079-Word Search(单词搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a 2D board and a word, find if ...

  9. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

随机推荐

  1. docker端口的映射顺序

    sudo docker run -d -p 8080:80 --name static_web jamtur01/static_web nginx -g "dameon off;" ...

  2. 微信小程序--问题汇总及详解之form表单

    附上微信小程序开发文档链接:https://mp.weixin.qq.com/debug/wxadoc/dev/framework/MINA.html form表单: 当点击 <form/> ...

  3. 谷歌插件请求ci 解决CI框架的Disallowed Key Characters错误提示

    用CI框架时,有时候会遇到这么一个问题,打开网页,只显示 Disallowed Key Characters 错误提示.有人说 url 里有非法字符.但是确定 url 是纯英文的,问题还是出来了.但清 ...

  4. 习题:过路费(kruskal+并查集+LCA)

    过路费  [问题描述]在某个遥远的国家里,有 n 个城市.编号为 1,2,3,…,n.这个国家的政府修 建了 m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市 T 需 要收取的过路费 ...

  5. php合并图片

    <?php class image{ /** * @param string $backgroundImage 背景图 * @param string $smallImage 小图 * @par ...

  6. 查找String中出现最多字符的次数和个数

    Sting 的charAt方法返回相应位置的字符,使用该方法遍历String,将每个字符存入对象属性,遍历属性得到最多字符个数 <!DOCTYPE html> <html> & ...

  7. div 的相对定位与绝对定位

    网 上也有不少使用div + css进行布局的教程,却存在很多缺陷,一是对css的布局模型讲解不清楚,让人很难理解相对定位.浮动等概念:二是虽然避免了“表格套表格”的缺点,却 带来了“div 套 di ...

  8. Linux下常用的命令记录

    本文章记录我在linux系统下常用或有用的系统级命令,包括软硬件查看.修改命令,有CPU.内存.硬盘.网络.系统管理等命令.但本文不打算介绍生僻命令,也不介绍各个linux发行版下的特有命令,且以后会 ...

  9. godaddy虚拟空间的伪静态配置

    原文发布时间为:2011-02-24 -- 来源于本人的百度文章 [由搬家工具导入] 只要在web.config文件的<configuration>子节点下 加入以下节点,即可实现 伪静态 ...

  10. 【HugeChm】HugeChm制作chm帮助文档

    1.下载软件:HugeChm.exe 2.开始打包:  3.选择开始打包即可