[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 ...
随机推荐
- angular 2+ 变化检测系列三(Zone.js在Angular中的应用)
在系列一中,我们提到Zone.js,Zones是一种执行上下文,它允许我们设置钩子函数在我们的异步任务的开始位置和结束位置,Angular正是利用了这一特性从而实现了变更检测. Zones.js非常适 ...
- 企业级中带你ELK如何实时收集分析Mysql慢查询日志
什么是Mysql慢查询日志? 当SQL语句执行时间超过设定的阈值时,便于记录到指定的日志文件中或者表中,所有记录称之为慢查询日志 为什么要收集Mysql慢查询日志? 数据库在运行期间,可能会存在这很多 ...
- Imcash平台测评报告
ImCash是由全球知名量子基金(QuantumFund)与美国好事达保险公司 (ALL ) 联合投资美国区块链金融资本(BFC)打造全球首款量子基金数字资产服务平台 . ImCash作为全球首款量子 ...
- Java内存溢出异常(上)
上一篇文章我们讲了JVM运行时数据区域与内存溢出异常,其中对于内存溢出异常这部分将的不够详细,这篇文章将着重讲解Java内存溢出异常的相关知识.如果有没看过上一篇文章的小伙伴们,请点击Java内存区域 ...
- react和redux版本不匹配
1.页面报错Cannot read property 'shape' of undefined 2. 原因为:react版本与react-redux版本不匹配. 1.package.json文件修改该 ...
- flask + MySQL-python 创建 webapp 应用
0 - python 用自带的 wgsi 也可以创建 web 服务1)建立 hello.py 内容如下 # hello.pydef application(environ, start_respons ...
- Android源代码编译过程及指令
编译Android源代码分为两种情况: 1. 完整编译源码: ./mk_aliphone.sh --> 完整编译脚本 --> 6735 输入对应的编号 --> userdebug ...
- 执行Android后台任务的最佳实践
灵活执行后台任务可以帮助提升应用性能,并最小化电量损耗. Android后台任务主题包含以下三个子主题: 1. 在IntentService中执行后台任务: 2. 使用CursorLoader在后台加 ...
- 关于MDN,HTML入门来自MDN文档
由开发者和作者组成的开源社区:推动web发展, MDN(Mozilla Developer Network) 维基,共同维护做贡献: 需要使用到github账号进行验证,以此再创建MDN账号: HTM ...
- BOM 浏览器对象模型_当前窗口的浏览历史 history 对象
当前窗口的浏览历史 window.history 对象 保存了当前窗口访问过的所有页面网址 由于安全原因,浏览器不允许脚本读取这些地址,但是允许在地址之间导航 history.back() 相当于 h ...