题目来源:

  https://leetcode.com/problems/word-break-ii/


题意分析:

  给定一个字符串s和一个字典dict(set),将所有将s有字典dict组成的结果输出。比如s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].那么结果是["cats and dog", "cat sand dog"]。


题目思路:

  我们将问题细化,如果s[:i]在字典dict里面,那么结果就是s[:i]和 solve(s[i + 1:],dict)的笛卡尔乘积。如果直接实现,那么时间复杂度较高,所以这里用动态规划的思想,判断一个字符串是否可以有字典组成。


代码(python):

class Solution(object):
def isp(self,s,dict):
dp = [False for i in range(len(s) + 1)]
dp[0] = True
for i in range(1,len(s) + 1):
for j in range(0,i):
if dp[j] and s[j:i] in dict:
dp[i] = True
return dp[len(s)]
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: Set[str]
:rtype: List[str]
"""
ans,tmp = [],""
if s in wordDict:
ans.append(s)
for i in range(len(s)):
tmp += s[i]
if tmp in wordDict:
if self.isp(s[i + 1:],wordDict):
t = self.wordBreak(s[i+1:],wordDict)
for j in t:
ans.append(tmp + " " + j)
return ans

[LeetCode]题解(python):140-Word Break II的更多相关文章

  1. LeetCode笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...

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

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

  3. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  4. 【LeetCode】140. Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  5. 【LeetCode】140. Word Break II 解题报告(Python & C++)

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

  6. [LeetCode] 140. Word Break II 单词拆分II

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

  7. 140. Word Break II

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

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

  9. leetcode 140. Word Break II ----- java

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

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

随机推荐

  1. bootstrap的导航改造

    在使用bootstrap制作后台时用到了响应式导航条,其中dropdown组件更是用的比较多,用的多需要点击的就多,dropdown默认鼠标左键单击才展开,如果使用鼠标放上去(hover)就展开则会省 ...

  2. 引用jquery框架后出错

    问题描述:当引用了jquery框架后,页面的js不能正常工作. 后面我的解决办法:是因为在引用 jquery的框架时的代码为 <script type="text/javascript ...

  3. dp状态压缩-铺砖问题

    题目:有一个n行m列的地板,需要用 1*2小砖铺盖,小砖之间互相不能重叠,问有多少种不同的铺法? 示范: 解法:用F[i][j]存放第i行的第j状态(j为十进制,转为二进制即是状态)有多少种方案. 用 ...

  4. hdu4507

    数位dp,终于守得云开见月明了.建议初学者先试试两道比较简单的hdu2089,hdu3555. 鸣谢:http://blog.csdn.net/acm_cxlove/article/details/8 ...

  5. ios 应用程序之间的跳转(内置程序的实现)

    ios 应用程序之间的跳转(内置程序的实现) 一个程序若要跳到另一个程序.需要在目标程序的plist文件里面修改: 打开info.plist,添加一项URL types 展开URLtypes,再展开I ...

  6. EasyUI特殊情况下的BUG整理

    前面有两篇: Easyui - combo[tree,box]下拉图标有间隙bug解决方法 http://blog.csdn.net/isea533/article/details/12996561 ...

  7. Android 数据库ORM框架GreenDao学习心得及使用总结<二>

    转:http://blog.csdn.net/xushuaic/article/details/24496191 第五篇 查询 查询会返回符合某些特定标准的实体.你可以使用原始的SQL定制查询语句,或 ...

  8. rownum的使用-分页

    rownum的使用-分页 oracle分页显示方法 一.使用rownum分页显示方式 方式1:SELECT *  FROM (SELECT ROWNUM r, a.* FROM b$i_exch_in ...

  9. JAVA仿真之银行出纳员

    学习例子是参照<thinking in java>中修改的,先贴上运行结果: 注意看红框之中的内容,这个仿真要达到这样一个目的: 1.客户队列(无优先级):每隔300MILLS生产一个客户 ...

  10. mac文件权限

    如何设置文件/或文件夹权限为777 进入终端,切换到指定目录,输入以下命令,后面添加你的文件名/目录名$sudo chmod -R 777 (文件名/目录名) 或 $chmod 777 ./test. ...