【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 ...
随机推荐
- Qt之资源系统
简述 Qt 的资源系统用于存储应用程序的可执行二进制文件,它采用平台无关的机制.当你的程序总需要这样的一系列文件(图标.翻译文件等)并且不想冒丢失某些文件的风险时,这就显得十分有用. 资源系统基于 q ...
- $('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法
$('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法 <input type='checkbox' id='cb'/> ...
- Clone Graph [LeetCode]
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- js仿京东轮播图效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- thinkjs——修改where默认条件为or
写之前,得先反思一下:总以为大神同事的高冷是一种与大家格格不入的节奏,可是当自己遇到的问题难以解决的时候,大神同事一下子就让问题迎刃而解,于是,满眼的敬佩之感:一定努力当一个像大神同事一样的progr ...
- oracle查锁表SQL
SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, s.user#, l.os_user_name,s.mach ...
- 使用jvisualvm.exe 的Btrace插件监控应用程序
之前提到使用命令行的方式执行btrace监控,其实jdk提供更好的方式监控应用程序. 我们可以使用jvisualvm.exe加插件的方式监控,这样更加方便. 1.在jvisualvm.exe安装btr ...
- 转: ORACLE索引介绍和使用
1.什么是索引 索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据: Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引: 索引由根节点.分支节点 ...
- POJ 1050 To the Max 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
- 2014北邮新生归来赛解题报告d-e
D: 399. Who Is Joyful 时间限制 3000 ms 内存限制 65536 KB 题目描述 There are several little buddies standing in a ...