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

  1. 【LeetCode OJ】Palindrome Partitioning

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...

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

  3. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  4. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  5. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  6. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  7. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. Ubuntu下配置和编译cpp-ethereum客户端

    Ethereum,中文翻译是“以太坊”,是一个公有区块链的开源项目.因为以太坊是基于P2P网络所以没有中心节点,所以用户仅安装Ethereum客户端即可连入Ethereum公共网络或者在自己的test ...

  2. vs2010 ctrl+F5闪退解决方法

    设置项目的属性页中的“配置属性”->“链接器”->“系统”->“子系统”->“控制台”(即增加“/SUBSYSTEM:CONSOLE”链接选项)

  3. 你不知道的JavaScript-- 事件流与事件处理

    转载:http://blog.csdn.net/i10630226/article/details/48970971 1. 事件处理 1.1. 绑定事件方式 (1)行内绑定 语法: //最常用的使用方 ...

  4. 自定义头文件 No such file or directory

    my_file.h为你的头文件名 要用#include"my_file.h",而不能是#include<my_file.h>. 如果头文件名在尖括号<>里, ...

  5. eclipse中 起动tomcat时报Multiple Contexts have a path of "/shopping"

    eclipse中 启动tomcat时报Multiple Contexts have a path of "/shopping". 这个是由于你的server服务器中的server. ...

  6. MATLAB画ROC曲线,及计算AUC值

    根据决策值和真实标签画ROC曲线,同时计算AUC的值 步骤: 根据决策值和真实标签画ROC曲线,同时计算AUC的值: 计算算法的决策函数值deci 根据决策函数值deci对真实标签y进行降序排序,得到 ...

  7. go局部变量的存储空间是堆还是栈?

    编译器会自动选择在栈上还是在堆上分配局部变量的存储空间,但可能令人惊讶的是,这个选择并不是由用var还是new声明变量的方式决定的. var global *int func f() { var x ...

  8. spring的bean管理

    1.所有的类都可以交给Spring管理 2.如何把一个类交给bean管理? (1)配置applicationContext.xml (2)在xml中写入bean节点配置要交给bean管理的类 3.程序 ...

  9. java 面向对象编程 第20章 XML技术解析

    1.  XML:extended Markup Language  可扩展标记语言,利用标签和子标签方式描述数据. 2.  声明<?xml version=”1.0”?>版本号 注释< ...

  10. 基于TCP协议的网络通信

    TCP/IP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信.Java对基于TC ...