[LC] 139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because"leetcode"
can be segmented as"leet code"
.
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because"
applepenapple"
can be segmented as"
apple pen apple"
.
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false Time: O(N^3)
Space: O(N)
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
can_break = [False] * (len(s) + 1) for i in range(1, len(s) + 1):
if s[:i] in wordDict:
can_break[i] = True
continue
for j in range(1, i):
if can_break[j] and s[j: i] in wordDict:
can_break[i] = True
break
return can_break[len(s)]
DFS
public class Solution {
public boolean canBreak(String input, String[] dict) {
Set<String> set = new HashSet<>(Arrays.asList(dict));
return helper(input, set, 0);
} private boolean helper(String input, Set<String> set, int index) {
if (index == input.length()) {
return true;
}
for (int i = index + 1; i <= input.length(); i++) {
if (set.contains(input.substring(index, i)) && helper(input, set, i)) {
return true;
}
}
return false;
}
}
public class Solution {
/*
* @param s: A string
* @param dict: A dictionary of words dict
* @return: A boolean
*/
public boolean wordBreak(String s, Set<String> dict) {
return helper(s, dict, 0);
} private boolean helper(String s, Set<String> dict, int index) {
if (index == s.length()) {
return true;
}
for (String str: dict) {
int len = str.length();
if (index + len > s.length()) {
continue;
}
if (!s.substring(index, index + len).equals(str)) {
continue;
}
if (helper(s, dict, index + len)) {
return true;
}
}
return false;
}
}
DP
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
boolean[] inDict = new boolean[s.length() + 1];
Set<String> set = new HashSet<>();
for (String word: wordDict) {
set.add(word);
}
inDict[0] = true;
for (int i = 1; i < inDict.length; i++) {
// substring(i) is beginIndex
if (set.contains(s.substring(0, i))) {
inDict[i] = true;
continue;
}
for (int j = 0; j < i; j++) {
if (inDict[j] && set.contains(s.substring(j, i))) {
inDict[i] = true;
break;
}
}
}
return inDict[inDict.length - 1];
}
}
[LC] 139. Word Break的更多相关文章
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 139. Word Break 以及 140.Word Break II
139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- 139. Word Break(动态规划)
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 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 ...
随机推荐
- 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学
编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...
- 题解 P1403 【[AHOI2005]约数研究】
题目 看到题解区很多人直接给出结论:答案为 \(\displaystyle \sum_{i=1}^n\lfloor{n\over i}\rfloor\) ,没给出证明,这里给出证明 [分析] 首先,我 ...
- JAVA 算法练习(一)
用java写了几道编程题目,分享给大家 语法和C语言几乎一样,不懂 java 会 c 也可以看明白的. 最大连续数列和 题目说明 对于一个有正有负的整数数组,请找出总和最大的连续数列.给定一个int数 ...
- 使用IDEA搭建SSM,小白教程
本片博客主要是搭建一个简单的SSM框架,感兴趣的同学可以看一下 搭建ssm框架首先我们需要有一个数据库,本篇文章博主将使用一个MySQL的数据,如果没学过MySQL数据库的,学过其他数据库也是可以的 ...
- AES学习小结
AES是基于数据块的加密方式,即每次处理的数据是一块(16字节),当数据不是16字节的倍数时填充,这就是所谓的分组密码(区别于基于比特位的流密码),16字节是分组长度. AES支持五种模式:CBC,C ...
- PAT Basic 反转链表 (25) [链表]
题目 给定⼀个常数K以及⼀个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5→6, ...
- PAT Advanced 1015 Reversible Primes (20) [素数]
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- ubuntu18.04国内软件源
ubuntu默认的软件源是国外的,下载软件很慢,需要更新为国内的源以提升速度,现在可以通过ubunt software来设置了,不过还是习惯了命令行修改的方式. 更新方法 123 sudo vi /e ...
- java使用forEach填充字典值
// 填充字典值 Vector vector = vectorMapper.selectByPrimaryKey(id); VectorModel vectorModel = new VectorMo ...
- win10 python 3.7 pip install tensorflow
环境: ide:pyCharm 2018.3.2 pyhton3.7 os:win10 64bit 步骤: 1.确认你的python有没有装pip,有则直接跳2.无则cmd到python安装目录下ea ...