【LeetCode OJ】Word Break
Problem link:
http://oj.leetcode.com/problems/word-break/
We solve this problem using Dynamic Programming method. Let A[0..n-1] be a boolean array, where A[i]=True if and only if s[i..n-1] can be segmented into words. The recursive formula is:
A[i] = True, if s[i..n-1] is a word
A[i] = True, if there exists j > i such that s[i..j-1] is a word and A[j] == True
A[i] = False, otherwise
We fill the A-table from i=n to 0, and return A[0] to tell if s[0..n-1] can be segmented into words.
(Note: there is another way that A[i] means if s[0..i] can be segmented, then the recursive formula becomes a little different, we fill the table from i=0 to n, and return A[n-1])
The pseudo-code is as follows
WORD-BREAK(string s, dictionary d):
let A[0..n-1] be a new array of False
for i = n-1 to 0
if A[i..n-1] is a word in d
A[i] = True
else
for j = i+1 to n-1
if A[j] == True and s[i..j-1] is a word in d
A[i] = True
break
return A[0]
And the following code is the python solution accepted by OJ.leetcode.
class Solution:
# @param s, a string
# @param dict, a set of string
# @return a boolean
def wordBreak(self, s, dict):
"""
We solve this problem using DP
Define a boolean array A[0..n-1], where
A[i] = True, means s[i..n-1] can be segmented into words
------------------------------------
The recursive formula is:
A[i] = True, if there exists j>i (s[i..n-1] = s[i..j-1] + s[j..n-1])
such that s[i..j-1] is a word and A[j] = True
or A[i] = True, if A[i..n-1] is a word
------------------------------------
We fill A-table from i=n-1 to n
"""
n = len(s)
A = [False] * n
i = n-1
while i >= 0:
if s[i:n] in dict:
A[i] = True
else:
for j in xrange(i+1, n):
if A[j] and s[i:j] in dict:
A[i] = True
break
i -= 1
return A[0]
【LeetCode OJ】Word Break的更多相关文章
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Word Ladder I
Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Convert Sorted List to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
随机推荐
- zju 1091
// Traveling Knight Problem #include "stdafx.h" #include <string> #include <strin ...
- sql 语句 截取字符串的两种方案
方案一:使用内置的函数 SUBSTRING,CHARINDEX,LEN三个内置函数 理论: SUBSTRING语法 SUBSTRING ( value_expression , start_exp ...
- poj1190 生日蛋糕(深搜+剪枝)
题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...
- SQL Server数据库(SQL Sever语言 CRUD)
使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...
- css中的zoom
CSS中zoom:1的作用兼容IE6.IE7.IE8浏览器,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:触发IE浏览器的haslayout解决ie下的浮动,margin重叠等一些问题. ...
- LevelDb简单介绍和原理——本质:类似nedb,插入数据文件不断增长(快照),再通过删除老数据做更新
转自:http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html 有时间再好好看下整个文章! 说起LevelDb也许您不清楚,但是如果作 ...
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
自己看了官方文档,也到网上查了下,目前理解如下: <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和Annotat ...
- .NET开发知识体系
记得几年前写过一篇关于.NET开发方面的知识总结,但是随着技术的发展以及自己技术理解的提升,觉得有必要对那篇文章加以更新和完善. 最近在园子里也看到有人写关于.NET知识体系的文章,特别是灵感之源写的 ...
- Group by的使用方法
sql中如果要分组查询,一般都会使用到group by语句,如何熟练使用group by语句呢,我分以下几点进行总结. Group by与聚合函数 Group by与Having 需要注意的地方 Gr ...
- System.Web.Optimization找不到引用
在程序包管理控制程序中录入:Install-Package Microsoft.AspNet.Web.Optimization,安装即可.