Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

解题思路一:

直接暴力枚举会导致TLE,因此,需要记录之前的结果,即可以采用dp的思路,JAVA实现如下:

  1. static public boolean wordBreak(String s, Set<String> wordDict) {
  2. boolean[] dp = new boolean[s.length() + 1];
  3. dp[0] = true;
  4. for (int i = 1; i < dp.length; i++)
  5. for (int j = i; j >= 0 && !dp[i]; j--)
  6. if (wordDict.contains(s.substring(i - j, i)))
  7. dp[i] = dp[i - j];
  8. return dp[dp.length - 1];
  9. }

解题思路二:

考虑到下题用dp做不出来,暴力枚举肯定TLE,所以可以设置一个unmatch集合来存储s中已经确定无法匹配的子串,从而避免重复检查,JAVA实现如下:

  1. static public boolean wordBreak(String s, Set<String> dict) {
  2. return wordBreak(s, dict, new HashSet<String>());
  3. }
  4.  
  5. static public boolean wordBreak(String s, Set<String> dict,
  6. Set<String> unmatch) {
  7. for (String prefix : dict) {
  8. if (s.equals(prefix))
  9. return true;
  10. else if (s.startsWith(prefix)) {
  11. String suffix = s.substring(prefix.length());
  12. if (!unmatch.contains(suffix)) {
  13. if (wordBreak(suffix, dict, unmatch))
  14. return true;
  15. else
  16. unmatch.add(suffix);
  17. }
  18. }
  19. }
  20. return false;
  21. }

Java for LeetCode 139 Word Break的更多相关文章

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

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

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

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

  3. Leetcode#139 Word Break

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

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

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

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

  6. Java for LeetCode 140 Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  7. LeetCode #139. Word Break C#

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  8. [leetcode]139. Word Break单词能否拆分

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

  9. [LeetCode] 139 Word Break(BFS统计层数的方法)

    原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...

随机推荐

  1. 【BZOJ-3172】单词 AC自动机

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2567  Solved: 1200[Submit][Status ...

  2. SpringMVC配置

    博客园 闪存 首页 新随笔 联系 管理 订阅 随笔- 4  文章- 1  评论- 0  搭建springmvc框架的另一种思路 在一个完整的项目里搭建springmvc框架的时候, 通常情况下,初学者 ...

  3. bzoj1069 SCOI2007 最大土地面积

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2560  Solved: 983 Description ...

  4. POJ3267 The Cow Lexicon(DP+删词)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9041   Accepted: 4293 D ...

  5. MVC执行顺序

    MVC在底层和传统的asp.net是一致的,在底层之上,相关流程如下: 1)Global.asax里,MvcApplication对象的Application_Start()事件中,调用 RouteC ...

  6. KxMenu下拉菜单

    + (void)createMenu:(id)sender target:(UIViewController *)t { NSArray *menuItems = @[ [KxMenuItem men ...

  7. Unity教程之再谈Unity中的优化技术

    这是从 Unity教程之再谈Unity中的优化技术 这篇文章里提取出来的一部分,这篇文章让我学到了挺多可能我应该知道却还没知道的知识,写的挺好的 优化几何体   这一步主要是为了针对性能瓶颈中的”顶点 ...

  8. centos linux从无到有安装wordpress

    序:本博客从无到有搭建wordpress,包括从服务器和域名购买,会将步骤一步一步记录下来.如果你也是新手,那你有福了,因为我的系统是centos,对号入座啊. 目录 一.准备域名和服务器一.安装ph ...

  9. linux命令合集

    序:介绍一些经常需要用到的Linux命令. 一.wget 作用:下载网络文件,将远程服务器文件恢复备份到本地. wget http://cn.wordpress.org/wordpress-3.1-z ...

  10. 读>>>>白帽子讲Web安全<<<<摘要→我推荐的一本书→1

      <白帽子讲Web安全>吴翰清著 刚开始看这本书就被这本书吸引,感觉挺不错,给大家推荐下,最近读这本书,感觉不错的精华就记录下, 俗话说>>>好脑袋不如一个烂笔头< ...