【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 ...
随机推荐
- CSS水平居中和垂直居中解决方案
一.CSS 居中 — 水平居中 DIV等标签本身没有定义自己居中的属性,网上很多的方法都是介绍用上级的text-align: center,然后嵌套一层DIV来解决问题. 可是这个方法有时候完全不起作 ...
- Visual Studio中的快捷键
我们在使用Visual Studio的时候,如用一些快捷键,就能减少我们键盘和鼠标来回切换的次数,从而提高我们编码的速度,在此跟大家分享一些经常Visual Studio中用到的快捷键 自动缩进:选中 ...
- 详解下一代开源混合应用框架Reapp(转)
http://www.iteye.com/news/30269 官网:http://reapp.io/ [开源推荐]Facebook开源的JavaScript库:React http://www.cs ...
- 20145236 《Java程序设计》第7周学习总结
20145236 <Java程序设计>第7周学习总结 教材学习内容总结 第十三章 时间与日期 认识时间与日期 时间的度量 格林威治标准时间GMT 格林威治标准时间的正午是太阳抵达天空最高点 ...
- c#和数据库技术基础之程序设计集散地-数据库
数据库,不明觉厉,今天我们就来数据库的神秘面纱 1.使用数据库的必要性 随着互联网技术的高速发展,截止到2013年我国网民的数量已接近6亿,网民数量的增长同事带动网上购物,微博,网络视频等新产业的发展 ...
- 采用SHELL,通过SQL LOAD导入一定格式的txt文件至数据库中
1. 准备工作,window中可直接通过sqlload直接导入文件,linux下,需要有sqlload的相关软件. 2. SQL脚本(MID_DFDZ.ctl) LOAD DATA INTO TABL ...
- async 和await
这个是.NET 4.5的特性,所以要求最低.NET版本为4.5. 看很多朋友还是使用的Thread来使用异步多线程操作,基本上看不见有使用Async.Await进行异步编程的.各有所爱吧,其实都可以. ...
- bash: sqlplus: command not found 解决方法
在oracle用户下输入:sqlplus 抛出bash: sqlplus: command not found 解决办法: 在root用户下输入如下命令: ln -s $ORACLE_HOME/bin ...
- jQuery.Autocomplete实现自动完成功能(详解)
1.jquery.autocomplete参考地址 http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ http://do ...
- 3D开发的基本知识
为了实现3D图形,程序员需要定义两个方面的数据: 1.3D图形的每个顶点(Vertex)的位置,每个顶点的位置都需要X.Y.Z三个左标值. 2.3D图形每个面由哪些顶点组成. Android的3D坐标 ...