[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 ...
随机推荐
- 201703-1 分蛋糕 Java
思路: 注意最后如果剩余蛋糕的重量小于k,也算一个人分到 import java.util.Scanner; public class Main { public static void main(S ...
- cpu压测测试--------自己设定cpu需要跑到的压力
下载压力测试包 https://pan.baidu.com/s/1DJYAzBHHDxMViy5dMel2Lw 提取码:a5j3 使用方法: 方法一:前端启动,按Ctrl+c结束 java -Dbus ...
- mint linux的几个问题
刚开机登录时, 键盘不停的输入同一个字符 发现在虚拟机里安装的系统没有问题, 因此怀疑配置有冲突, 新建一个用户果然能解决问题 解决方法: 把 主目录下, 隐藏的配置目录删除, 重新登录后再配置. 4 ...
- selenium请求豆瓣网
#请求豆瓣网 from selenium import webdriverimport timedriver = webdriver.Chrome() driver.get("http:// ...
- UVa-679 Dropping Balls 二叉树
题目链接:https://vjudge.net/problem/UVA-679 题意: 有一棵二叉树,所有节点从上至下,从左到右依次编号为1.2...2D-1,叶子深度都相同,有I个小球,从根节点依次 ...
- h5-localStorage储存的使用
<!-- localStorage的使用: 1.存储的内容大概20mb 2.不同浏览器不能共享数据,但是在同意浏览器的不同窗口中可以共享数据 3.永久生效,他的数据是储存在硬盘上,并不会随着页面 ...
- PHP的isset()函数 一般用来检测变量是否设置
PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...
- 标准库模块——shutil模块
shutil.rmtee 删除目录及以内的所有文件. import shutil shutil.rmtree(r'D:\python\222') #包括222在内的所有文件全部删除.
- Go-map-字符串-指针-结构体
Maps 什么是 map ? 类似Python中的字典数据类型,以k:v键值对的形式. map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值. 如何创建 ma ...
- python爬虫王者荣耀高清皮肤大图背景故事通用爬虫
wzry-spider python通用爬虫-通用爬虫爬取静态网页,面向小白 基本上纯python语法切片索引,少用到第三方爬虫网络库 这是一只小巧方便,强大的爬虫,由python编写 主要实现了: ...