[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 ...
随机推荐
- 《C Primer Plus》- 第二章 C语言概述
本笔记写于2020年1月27日. 本系列文章参考的是<C Primer Plus>(第六版),其中里面会有笔者自己的相关补充. 以下示例均运行于macOS Catalina 10.15.2 ...
- 随机森林RF
bagging 随机森林顾名思义,是用随机的方式建立一个森林,森林里面有很多的决策树组成,随机森林的每一棵决策树之间是没有关联的.在得到森林之后,当有一个新的输 入样本进入的时候,就让森林中的每一棵决 ...
- 2.react 基础 - create-react-app 目录结构 及 组件应用
1. react-app 脚手架的 目录结构 node_modules -d 存放 第三方下载的 依赖的包 public -d 资源目录 favicon.ico - 左上角的图标 index.h ...
- Eclipse Springboot项目Dokcer
配置好Dockerfile FROM openjdk:8-jdk-alpine ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOIN ...
- thinkcmf面包屑制作
网站常有的功能面包屑,如图所示: 支持1级分类 list.html 首页 / {$name} article.html 首页 / {$term['name']} / {$post_title} 链接: ...
- 吴裕雄--天生自然 JAVA开发学习:条件语句
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out. ...
- UML-领域模型-添加关联和属性
1.何谓关联? 关联(association):一个类的全局变量引用了另一个类,就表示关联了这个类 2.何时使用关联? 长时间(需要记住)留存的需要关联:短时间的不需要.比如: 需要关联:老师教那些课 ...
- 通过OAuth2.0 获取授权访问SF 用户数据
站长资讯: 创建应用程序 新建应用程序 访问示例(Python+django) 环境准备: index.html 两种方式: 方式一:采用由用户授权,调用者无需知道SF的用户名与密码 方式二:直接 ...
- drf偏移分页组件-游标分页-自定义过滤器-过滤器插件django-filter
drf偏移分页组件 LimitOffsetPagination 源码分析:获取参数 pahenations.py from rest_framework.pagination import Limit ...
- [Scoi2014]方伯伯的OJ(动态开点splay)
开始没看数据范围差点以为是这题了:https://www.cnblogs.com/hfctf0210/p/10911340.html 然后看到n<=1e8,怎么这么大? 所以这题需要用动态开点线 ...