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

解题思路一:

直接暴力枚举会导致TLE,因此,需要记录之前的结果,即可以采用dp的思路,JAVA实现如下:

	static public boolean wordBreak(String s, Set<String> wordDict) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 1; i < dp.length; i++)
for (int j = i; j >= 0 && !dp[i]; j--)
if (wordDict.contains(s.substring(i - j, i)))
dp[i] = dp[i - j];
return dp[dp.length - 1];
}

解题思路二:

考虑到下题用dp做不出来,暴力枚举肯定TLE,所以可以设置一个unmatch集合来存储s中已经确定无法匹配的子串,从而避免重复检查,JAVA实现如下:

    static public boolean wordBreak(String s, Set<String> dict) {
return wordBreak(s, dict, new HashSet<String>());
} static public boolean wordBreak(String s, Set<String> dict,
Set<String> unmatch) {
for (String prefix : dict) {
if (s.equals(prefix))
return true;
else if (s.startsWith(prefix)) {
String suffix = s.substring(prefix.length());
if (!unmatch.contains(suffix)) {
if (wordBreak(suffix, dict, unmatch))
return true;
else
unmatch.add(suffix);
}
}
}
return false;
}

Java for LeetCode 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 、140. Word Break II

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

  3. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  4. leetcode 139. Word Break ----- java

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

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

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

  6. Java for LeetCode 140 Word Break II

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

  7. LeetCode #139. Word Break C#

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

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

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

  9. [LeetCode] 139 Word Break(BFS统计层数的方法)

    原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...

随机推荐

  1. Android中实现自定义的拍照应用

    可以参考:http://www.android-doc.com/guide/topics/media/camera.html 一.添加相应的权限 <uses-permission android ...

  2. BZOJ-1877 晨跑 最小费用最大流+拆点

    其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...

  3. POJ-2777Count Color 线段树+位移

    这道题对于我这样的初学者还是有点难度的不过2遍A了还是很开心,下面说说想法-- Count Color Time Limit: 1000MS Memory Limit: 65536K Total Su ...

  4. 【bzoj1037】 ZJOI2008—生日聚会Party

    http://www.lydsy.com/JudgeOnline/problem.php?id=1037 (题目链接) 题意 有n个boy和m个girl排成一排,求使得任意一段的boy个数girl个数 ...

  5. BZOJ4241 历史研究

    Description IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记.JOI教授为了通过这份日记来研究古代IOI国的生活,开始着手调查日记中记载的事件. ...

  6. 洛谷P1203 [USACO1.1]坏掉的项链Broken Necklace

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  7. POJ2823 Sliding Window

    Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53086   Accepted: 15227 Case Time Limi ...

  8. 以一个权限系统来告别WebForm —(一)项目整休架构设计与数据库设计

    在本节我想与大家与分享一下,我所将要做的权限系统的架构和数据库的表的设计.请各位大神们对我项目中设计的不足之处进行指导,让我得以更好的写完它,留给需要它的人. 我的项目架构如下图所示: 如上图所示,在 ...

  9. 数组添加:如何往数组的"null"位置插入数据呢?

    数组添加,当已经存在的一个数组时,如何往数组的"null"位置插入数据呢? 分析: 1.循环遍历数组元素,找出null的位置(下标) 2.设置一个变量,接收null位置下标值 3. ...

  10. bootstrap学习总结-06 按钮

    一按钮的基本样式 Bootstrap提供一组标准的按钮配色和大小调整方案,只需要简单的应用的按钮类即可.BootStrap3提供了按钮的标准样式如图. <!DOCTYPE html> &l ...