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. Python的 5 种高级用法,效率提升没毛病!

    任何编程语言的高级特征通常都是通过大量的使用经验才发现的.比如你在编写一个复杂的项目,并在 stackoverflow 上寻找某个问题的答案.然后你突然发现了一个非常优雅的解决方案,它使用了你从不知道 ...

  2. 【每日Scrum】第七天冲刺

    一.计划会议内容 界面ui制作,主界面进度 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图  

  3. centos rpm安装jdk1.8

    1.官网下载jdk的rpm文件(http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) ...

  4. rocketmq 使用

    rocketmq  基本使用可以看官网和官网给的demo. https://github.com/apache/rocketmq/tree/master/example 这里主要说明几个点:rocke ...

  5. javaweb03 javaservlet基础一

    1.使用JavaEE版的eclipse开发动态的WEB工程(JavaWEB 项目)1).把开发选项切换到JavaEE2).可以在window -> Show View 中找到Package Ex ...

  6. pycharm调试、设置汇总

    目录: 1.pycharm中不能run 2.pycharm基本调试操作 3.pycharm使用技巧 4.pycharm Error running draft: Cannot run program ...

  7. IO流的学习以及统计次数最多的单词

    IO流: 处理数据类型:字节流(InputStream  OutputStream)和字节流(Reader  Writer) 数据流向不同:输入流和输出流(FileInputStream   File ...

  8. Kafka学习(学习过程记录)

    Apache kafka 这,仅是我学习过程中记录的笔记.确定了一个待研究的主题,对这个主题进行全方面的剖析.笔记是用来方便我回顾与学习的,欢迎大家与我进行交流沟通,共同成长.不止是技术. Kafka ...

  9. Scanner方式输入小写字母转换成大写字母

    import java.util.Scanner; /**  * 小写字母转换成大写字母      * @author zzu119  *  */ public class letterTransfe ...

  10. python3 str.encode bytes.decode

    str.encode 把字符串编码成字节序列 bytes.decode 把字节序列解码成字符串 https://docs.python.org/3.5/library/stdtypes.html st ...