【LeetCode OJ】Palindrome Partitioning II
Problem Link:
http://oj.leetcode.com/problems/palindrome-partitioning-ii/
We solve this problem by using Dynamic Programming.
Optimal Sub-structure
Assume a string S has the palindrome minimum cuts n, and S = W1 + W2 + ... + Wn where Wi is a palindrome. Then for S' = W1 + W2 + ... + Wn-1, S' must have the palindrome minimum cut n-1. It is easy to prove by the contradiction.
Recursive Formula
Given a string s, let A[0..n-1] be an array where A[i] is the palindrome minimum cuts for s[0..i]. The recursive formula for A[] is:
A[0] = 0, since an empty string is a palindrome
For i > 0, we have
A[i] = 0, if s[0..i] is a palindrome
A[i] = min{ A[j]+1 | j = 1, ..., i and s[j+1..i] is a palindrome }, otherwise
Implementation
The following code is the python impelmentation accepted by oj.leetcode.com
class Solution:
# @param s, a string
# @return an integer
def minCut(self, s):
"""
Let A[0..n-1] be a new array, where A[i] is the min-cuts of s[0..i]
A[0] = 0, since "" is a palindrome
For i > 0, we have
A[i] = 0, if s[0..i] is palindrome
A[i] = min{ A[j]+1 | 0 < j <= i }, otherwise
"""
n = len(s)
# n = 0 or 1, return 0, no cut needed
if n < 2:
return 0 # Initialization: s[0..i] at least has i cuts to be partitioned into i characters
A = range(n)
for i in xrange(n):
A[i] = i # Compute P: P[i][j] = True if s[i..j] is a palindrome
P = [None] * n
for i in xrange(n):
P[i] = [False] * n for mid in xrange(n):
P[mid][mid] = True
# Check strings with mid "s[mid]"
i = mid - 1
j = mid + 1
while i >= 0 and j <= n-1 and s[i]==s[j]:
P[i][j] = True
i -= 1
j += 1
# Check strings with mid "s[mid]s[mid+1]"
i = mid
j = mid + 1
while i >= 0 and j <= n-1 and s[i] == s[j]:
P[i][j] = True
i -= 1
j += 1 # Quick return, if s[0..n-1] is a palindrome
if P[0][n-1]:
return 0 # DP method, update A from i = 1 to n-1
for i in xrange(n):
if P[0][i]:
A[i] = 0
else:
for j in xrange(i):
if P[j+1][i]: # s[0..i] = s[0..j] + s[j+1..i], where s[j+1..i] is a palindrome
A[i] = min(A[i], A[j]+1) return A[n-1]
【LeetCode OJ】Palindrome Partitioning II的更多相关文章
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- cmd界面的编码如何改为utf8
在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即 ...
- cf--------(div1)1A. Theatre Square
A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...
- emmet使用笔记及sublime常用快捷键
2015.10.07补: 格式化JS代码: 安装JSformat插件,选中需要格式化的JS代码,Ctrl+Alt+f 使用笔记看:http://www.w3cplus.com/tools/emmet- ...
- Objective-C:Foundation框架-常用类-NSMutableString
NSString是不可变的,不能删除字符或修改字符,它有一个子类NSMutableString,为可变字符串. NSMutableString的两种创建方法: - (id) initWithCapac ...
- Nodejs异步异常处理domain
前言 程序开发中,最麻烦的事情之一就是异常处理:对于Nodejs程序开发,最麻烦的事情莫过于异步异常处理. 以MVC的多层架构设计角度,异常总是要一层一层向上抛出,最后在客户端出打印错误.但是,Nod ...
- struts2视频学习笔记 11-12(动态方法调用,接收请求参数)
课时11 动态方法调用 如果Action中存在多个方法时,可以使用!+方法名调用指定方法.(不推荐使用) public String execute(){ setMsg("execute&q ...
- 开源软件架构总结之——Bash(readline做输入交互式,词法语法分析,进程交互)
第3章 The Bourne-Again Shell Bash的主要组件:输入处理,解析,单词展开(word expansion)和其他命令处理,管道(pipeline)中的命令执行.这些组件构成一个 ...
- Spring源码学习-PropertyPlaceholderHelper
转载:http://my.oschina.net/ydsakyclguozi/blog/465526 1. CustomPropertyConfigurer.java package property ...
- svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits svynsync:E170009
svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits; consider u ...
- mac 安装memcached服务
使用homebrew安装,homebrew安装方法http://brew.sh/ 安装memcached服务 brew install memcached 配置开机启动(用brew安装之后下面会提示怎 ...