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的更多相关文章

  1. leetcode 139. Word Break 、140. Word Break II

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

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

  3. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. Leetcode#139 Word Break

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

  5. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  6. [LeetCode] 139. Word Break 单词拆分

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

  7. 139. Word Break(动态规划)

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

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

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

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

随机推荐

  1. 施魔法(DP)

    链接:https://ac.nowcoder.com/acm/contest/3003/H来源:牛客网 题目描述 牛可乐有 n 个元素( 编号 1..n ),第 i 个元素的能量值为 ai​. 牛可乐 ...

  2. UML-设计模式-本地服务容错-适配器+工厂模式

    问题1:我们的ProductCatalog存储在了数据库里了,但是数据库瘫掉了,怎么办? 解决:本地(Map)---->Local(文件)---->DB 问题2:如果新加了存储Produc ...

  3. gitee上传下载代码命令

    在想要下载的文件夹下,鼠标右键,git bash 1.输入git init 进行初始化 2.git remote add origin https://gitee.com/XXXXXXXXXXXXXX ...

  4. [NSConcreteValue doubleValue]: unrecognized selector sent to instance

    今天需求说要给在进入某个页面给某个按钮加上放大效果,心想这还不简单,于是三下五除二的把动画加上提交测试了. 下面是动画的代码 NSTimeInterval time = CACurrentMediaT ...

  5. Python笔记_第一篇_面向过程_第一部分_2.内存详解

    Python的很多教材中并没有讲内存方面的知识,但是内存的知识非常重要,对于计算机工作原理和方便理解编程语言是非常重要的,尤其是小白,因此需要把这一方面加上,能够更加深入的理解编程语言.这里引用了C语 ...

  6. Python笔记_第一篇_面向过程_第一部分_1.Python环境的设置(含虚拟机)

    *Python环境的设置 Python的环境设置也就是所需工作平台的软件搭建.常用的搭建平台IOS系统+Linux系统和Windows+Linux系统的搭建.这里主要说明的是Windows+Linux ...

  7. DVWA-目录遍历-文件包含

    开门见山 · 目录遍历 替换成 2. 文件包含可以使用 绝对路径 也可以 3. 可以使用文件包含来包含一个网址,或者是一个shell 远程文件 空字符绕过字符过滤 %00

  8. CodeForces - 1243D. 0-1 MST(补图连通分量个数)

    Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: ...

  9. c语言中多维数组和指针的关系

    如图: 执行结果: 说明:由执行结果可知,三个输出的结果相等(可能在不同的平台执行结果不相同,但三个的结果是相等的),数组multi的地址与数组multi[0]的地址相同,都等于存储的第一个整数的地址 ...

  10. java8新特性-函数式接口详细讲解及案例

    一.函数式接口 1.1 概念 函数式接口在Java中是指:有且仅有一个抽象方法的接口.函数式接口,即适用于函数式编程场景的接口.而Java中的函数式编程体现就是Lambda,所以函数式接口就是可 以适 ...