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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

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

  5. [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 ...

  6. [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 = ...

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

  8. [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming

    Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...

  9. [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 ...

随机推荐

  1. 不输入密码执行SUDO命令

    假如我们需要用户名nenew执行sudo时不用输入密码 1.打开sudoers: visudo /etc/sudoers 2.在文件的最后一行添加: nenew    ALL=(ALL) NOPASS ...

  2. Doctirne---查询更新等操作

    使用Doctrine进行mysql更删改查操作,事务处理,生命周期的管理 1.先记录最简单的插入操作 $em = $this->getDoctrine()->getManager(); / ...

  3. Java 使用blob对H5视频播放进行加密《java视频加密》

    1.创建一个H5 <video>标签 <video id="sound" type="video/mp4" controls="co ...

  4. mysql 正则表达式问号

    MySQL 中,用正则表达式匹配?,需要使用两个转义字符   \\? 因使用一个被坑了很久.

  5. vscode断点调试本地客户端文件

    一.安装chrome,安装vscode,打开vscode编辑器,安装插件Debugger for Chrome 二.新建文件 1.目录结构 . ├── index.html ├── index.js ...

  6. Jquery中事件的重复绑定

    问题:事件的绑定具有叠加效果,已有绑定的函数时,再次绑定,执行时会出现绑定多少次,触发多少次的情况 解决:1.on();绑定事件,提供了绑定事件处理程序所需的所有功能,用于统一取代以前的bind(). ...

  7. 使用163.com邮箱发送邮件

    不要直接使用登录的密码,而是用配置中的授权码做为密码

  8. Python编码规范:IF中的多行条件

    Python编码规范:IF中的多行条件 转载 2017年03月08日 09:40:45 http://blog.csdn.net/wsc449/article/details/60866700 有时我 ...

  9. MyBatis入门(二)接口式编程

    在  MyBatis入门(一) 的基础之上编写接口 将接口和Mapper文件进行绑定,会为接口创建一个代理对象,代理对象去执行增删改查 (1)编写接口 public interface EmpDao ...

  10. __x__(30)0908第五天__导航条的练习 <div>版本

    效果图:  html源代码: <!doctype html> <html> <head> <meta charset="utf-8" /& ...