[LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
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 这个题目利用dynamic programming,因为是问yes/no,并且跟坐标有关。利用mem, mem[i] means whether the first i characters can be segment,
mem[i] = Or(mem[j] and s[j:i] is in WordDict). 需要注意的是/可以提高效率的是,跟[LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming不同的是第二个
loop不需要每次都从0开始,因为如果我们知道dictionary中最大长度的word,只需要从i - maxlength来判断即可,然后当i 很小的时候有可能小于0, 所以用max(0,i - maxlength)来作为起始点。 T: O(n * maxl * maxl * len(wordDict)) # 前面n * maxl 因为两个loop,maxl * len(wordDict) 是判断一个string是否在wordDict里面的时间。
Code:
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
# Dynamic programming, T: O(len(s)*l*l*len(wordDict)) S: O(len(s))
maxl, n = 0, len(s)
for word in wordDict:
maxl = max(maxl, len(word))
mem = [False] * (n + 1)
mem[0] = True # mem[i] means whether the first i characters can be segment
for i in range(1, n + 1):
for j in range(max(0, i - maxl), i): #because the word been check should be at most with maxl length
if not mem[j]:
continue
if s[j:i] in wordDict:
mem[i] = True
break
return mem[n]
[LeetCode] 139. Word Break_ Medium tag: Dynamic Programming的更多相关文章
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming
Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...
- [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- python之cookie, cookiejar 模拟登录绕过验证
0.思路 如果懒得模拟登录,或者模拟登录过于复杂(多步交互或复杂验证码)则人工登录后手动复制cookie(或者代码读取浏览器cookie),缺点是容易过期. 如果登录是简单的提交表单,代码第一步模拟登 ...
- UOJ#185. 【ZJOI2016】小星星 容斥原理 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ185.html 题解 首先暴力DP是 $O(3^nn^3)$ 的,大家都会. 我们换个方向考虑. 假设我们 ...
- MySQL通过Navicat实现远程连接的过程
直接使用Navicat通过IP连接会报各种错误,例如:Error 1130: Host '192.168.1.80' is not allowed to connect to this MySQL ...
- 《XXX重大技术需求征集系统》的可用性和可修改性战术分析
在网站的界面完整有效的呈现在最终用户面前前,其中经历的每一环节出现问题都会导致网站页面不可访问.原因如,如DNS被劫持.网站交换机失效,硬盘损坏,网卡松掉,机房停电等都可能导致网站不可用(网站故障)情 ...
- python经典书籍推荐:python编码规范
目录INF-qa Python 编码规范................................................................................ ...
- Mybatis内批量插入Oracle
<insert id="insertManagerInfoBatch" parameterType="java.util.List"> <se ...
- [Python]Python中的浅复制与深复制
看python的文档,发现list有copy方法,描述说效果同a[:]. 感觉有点惊讶,之前一直以为a[:]执行的是深复制. test了一下,发现确实如果a中存在可变对象,如list/set/dict ...
- 潭州课堂25班:Ph201805201 tornado 项目 第九课 深入应用 WebSockets(课堂笔记)
tornado 相关说明 在 handler 中创建一个 chat.py 文件,用来处理聊天室 在 templates 模板文件夹下创建 room.html 文件,是个聊天室 做好服务器的准备
- I Think I Need a Houseboat POJ - 1005
I Think I Need a Houseboat POJ - 1005 解题思路:水题 #include <iostream> #include <cstdio> #inc ...
- 听说尤雨溪在开发vue4.0?是谁煽动了前端圈的焦虑情绪
导火索因P图而起 今天前端圈里被一张P图搞得好热闹,最初只是QQ群里一个冒名尤雨溪的前端网友发了一句调侃的话,原话截图如下: 看完觉得好搞笑,说尤雨溪在开发vue4.0,有谁学不动了,就给他发10块钱 ...