【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

时间:2021-07-28 00:23:01

【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;
}
}

评測结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47774547

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】的更多相关文章

  1. &lbrack;LeetCode&rsqb; 139&period; Word Break 单词拆分

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

  2. LeetCode 139&period; Word Break单词拆分 &lpar;C&plus;&plus;&rpar;

    题目: 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. &lbrack;leetcode&rsqb;139&period; Word Break单词能否拆分

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

  5. Leetcode139&period; 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. Linux系统性能统计工具Sar和实时系统性能监控脚本

    sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...

  2. SPRING IN ACTION 第4版笔记-第一章-005-Bean的生命周期

    一. 1. As you can see, a bean factory performs several setup steps before a bean is ready touse. Let’ ...

  3. php防止sql注入函数

    $magic_quotes_gpc = get_magic_quotes_gpc(); @extract(daddslashes($_COOKIE)); @extract(daddslashes($_ ...

  4. angularJS怎么实现与服务端的PHP进行数据交互

    //{params: 要传的参数obj },params这个是关键字不能换别的变量 $http.get(url, {params: {id: categoryid, key: keys} }).suc ...

  5. 学习笔记——Java核心技术之接口、继承与多态练习题

    1.创建一个抽象类,验证它是否可以实例化对象. package com.lzw; public abstract class UseCase3 { abstract void doit(); publ ...

  6. jPlayer获取播放时间

    关于jPlayer的用法,可以参考:jPlayer 2.6.0开发者手册 http://www.jplayer.cn/developer-guide.html 视频播放例子: //视频播放 var v ...

  7. 引用计数——深拷贝&amp&semi;浅拷贝

    下面是用代码实现: private: char *data; size_t use_count; public: //构造函数 String_rep() { if(str == NULL) { dat ...

  8. Ext4文件系统架构分析&lpar;一&rpar;

    本文描述Ext4文件系统磁盘布局和元数据的一些分析,同样适用于Ext3和Ext2文件系统,除了它们不支持的Ext4的特性外.整个分析分两篇博文,分别概述布局和详细介绍各个布局的数据结构及组织寻址方式等 ...

  9. vue的面包屑导航组件

    用来将其放到navbar中: Breadcrumb/index.vue <template> <el-breadcrumb class="app-breadcrumb&qu ...

  10. 【&period;net开发者自学java系列】使用Eclipse开发SpringMVC(3)

    [.net开发者自学java系列]使用Eclipse开发SpringMVC(3) 标签(空格分隔): Spring RESTful 很久没继续学习java的spring了.接下来继续 回忆一下上个随笔 ...