题目要求

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.Return all such possible sentences.For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].A solution is ["cats and dog", "cat sand dog"].

现在有一个非空字符串s和一个非空的字典wordDict。现在向s中添加空格从而构成一个句子,其中这个句子的所有单词都存在与wordDic中。字典中不包含重复的单词。

思路和代码

我们只需要每次从当前字符串中进行一次分割,该分割保证前一部分为字典中的一个单词,然后我们将后一部分作为子字符串递归的传入方法获取子字符串的分割。

catsanddog
cat | sanddog
cat | sand | dog
cats | anddog
cats | and | dog

但是单纯的递归会带来超时的情况,尤其是当字典中的单词出现大量包含的情况,如:[a,aa,aaa,aaaa],而输入为aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
我们可以利用HashMap缓存来解决这个问题,其中key为子字符串,value为其分割的结果的列表。

    private Map<String, List<String>> cache = new HashMap<String, List<String>>();public List<String> wordBreak(String s, List<String> wordDict) {if(cache.containsKey(s)) return cache.get(s);List<String> result = new ArrayList<String>();if(s.length()==0){result.add("");return result;}for(String word : wordDict){if(s.startsWith(word)){List<String> subWords = wordBreak(s.substring(word.length()), wordDict);for(String subWord : subWords){result.add(word + (subWord.isEmpty() ? "" :" ") + subWord);}}}cache.put(s, result);return result;}


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

leetcode140. Word Break II相关推荐

  1. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  2. Word Break Word Break II

    leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...

  3. [LeetCode] Word Break II 拆分词句之二

    [LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...

  4. leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)

    题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...

  5. LeetCode Word Break II

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

  6. 140. Word Break II

    文章目录 1 题目理解 2 回溯+记忆化 1 题目理解 140与130的区别是,当字符串可分的时候,要求返回具体的分割字符串. 2 回溯+记忆化 对于字符串s,如果前面一部分是单词列表中的词,则拆分出 ...

  7. LeetCode: Word Break II [140]

    [题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...

  8. word break II 对字符串根据已知字典 分解出所有可能组合

    采用暴力法(brute force 简称BF,普通模式匹配) 思路:1,首先判断字符串的第一个字符在不在字典里,如果在,判断第二个字符在不在字典里,如果不在,前两个字符在不在字典里,依次迭代; 2,这 ...

  9. Leetcode word break I II 词句拆分I和II的java实现及解析

    Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...

最新文章

  1. [php]如何做到高并发优化
  2. a=a+b与a+=b有什么区别吗?
  3. 项目经理怎么运用思维导图
  4. Nginx下完美解决WordPress的伪静态 (wordpress 迁移后 导致 页面404)
  5. 抓取流量分析恶意软件或泄密流量(精)
  6. pandas read_sql
  7. 【转载】随机游走及马尔科夫链(本体映射相关)
  8. Bootstrap3基础 栅格系统 标尺(col-lg/md/sm/xs-1)
  9. 2017下半年,一二线互联网公司Android面试题汇总
  10. TranslateAnimation解析
  11. Internet浏览器,C:\fakepath路径问题
  12. 免费的Bootstrap管理后台模板集合
  13. matlab 三角函数 积化和差,三角函数积化和差与和差化积公式
  14. HBase Java API操作数据库
  15. dsp28335杂记2
  16. 战神引擎mud2没有mysql文件_战神引擎架设不要(MongoDB)芒果数据库配置教程
  17. 【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
  18. pytorch RuntimeError: size mismatch, m1: [64 x 784], m2: [784 x 10] at
  19. Linux 源码分析 之 udp 分析 二 sendto
  20. linux vdi虚拟化,VDI桌面虚拟化解决方案

热门文章

  1. r语言ggplot2一夜多图_跟着Nature microbiology学画图~R语言ggplot2画直方图
  2. swiper.js插件的使用
  3. Python升级pip并安装opencv、moviepy包
  4. JSP的MVC模式(JavaBean、Servlet、JSP)
  5. 2020计算机大纲,计算机专业2020考试大纲.doc
  6. 小度智能音箱维修点_小度智能音箱——联通智慧生活语音服务入口
  7. connection timed out是什么意思_为什么Java中1000==1000为false而100==100为true?
  8. redis数据结构小结
  9. dll修改和EXCOPE
  10. 光影之路 GPU架构发展史(1/4)