Given a string s, cut s into some substrings such that every substring is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

Given s = "aab",

Return 1 since the palindrome partitioning ["aa", "b"] could be produced using 1 cut.

For this problem, the minimum cuts to achieve all single palindrome words could be defined as f[n]. To solve this problem, the dynamic programming is needed to be used. For any integer from 0 to i-1, the f[i] is depend on the minimum value of f[j] + 1 becuase if there any value less than it will update it.

 public class Solution {
/**
* @param s a string
* @return an integer
*/
public int minCut(String s) {
if (s==null || s.length() == 0 ) {
return 0;
}
int n = s.length();
//preprocessing to store all the sub-string's boolean palindrome feature
boolean[][] isPalindrome = getIsPalindorme(s);
int[] f = new int[n+1];
//initialize
for (int i =0; i <= n; i++) {
f[i] = i-1;
}
//DP
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++ ) {
if (isPalindrome[j][i-1]) {
f[i] = Math.min(f[i],f[j]+1);
}
}
}
return f[n];
}
//this method is used to preprocess the result whether it is a panlindrome string of
//the certain substring from index i to index j and store them all into a matrix
public boolean[][] getIsPalindorme(String s) {
int length = s.length();
boolean [][] isPalindrome = new boolean[length][length];
//initialize for all single characters
for (int i = 0; i < length; i++) {
//this means all the single character in the string could be used as
//a palindrome word
isPalindrome[i][i] = true;
}
//initialize for all two neighbor characters
for (int i = 0; i < length-1; i++) {
isPalindrome[i][i + 1] = (s.charAt(i) == s.charAt(i + 1));
}
//develop for all result from start to start + lengthj indexs subtring
for (int delta = 2; delta <= length -1; delta++) {
for (int start = 0; start + delta < length; start++) {
//the result of the string from index start to start+length
//is based on the result of string with index start+1 to start+length-1
//and and the two chars at start indexa and start +length index are equal
isPalindrome[start][start + delta]
= isPalindrome[start + 1][start + delta - 1] && s.charAt(start) == s.charAt(start + delta);
}
}
return isPalindrome;
}
}

LintCode Palindrome Partitioning II的更多相关文章

  1. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

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

  2. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  3. 【leetcode】Palindrome Partitioning II

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

  4. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  5. [LeetCode] Palindrome Partitioning II 解题笔记

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

  6. 动态规划——Palindrome Partitioning II

    Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...

  7. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  8. LeetCode: Palindrome Partitioning II 解题报告

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

  9. 【LeetCode】132. Palindrome Partitioning II

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

随机推荐

  1. jquery 巧用json传参

    JavaScript代码,巧用JSON传参数function AddComment(content) { var comment = {}; comment.threadId = $("#s ...

  2. placeholder 颜色更改

    ::-webkit-input-placeholder { /* WebKit browsers */ color: #999; } :-moz-placeholder { /* Mozilla Fi ...

  3. Java中使用二重循环打印沙漏图形

    1.首先判断外层,A .B.C.D都符合条件 2.那么再看内层 A :int i=0;i<5;i++ 当i=1时;带入到第二个内层循环中 int j=0;j<Math.abs(i)*2+1 ...

  4. vs2012 error c4996: This function or variable may be unsafe

    编译lua源码时,使用vs2012,遇到如下错误. 1>------ 已启动生成: 项目: 20130925, 配置: Debug Win32 ------ 1>  stdafx.cpp ...

  5. Ionic的跨域问题

    跨域大家都不陌生,但最近一直遇到一个坑,也是自身对ajax和angular的不深入造成,所以记录一笔,下次遇到绕过. 参考过:http://ionichina.com/topic/54f051698c ...

  6. 编程key note

    一些日常发现的code better的要点.不断更新. * #include <assert.h> 使用断言* 每个模块(文件)应该有一个唯一的一个前缀,模块导出的所有全局名字都应以此前缀 ...

  7. Hibernate2

    计应134(实验班) 杨伟 Hibernate 中提供了两级Cache(高速缓冲存储器),第一级别的缓存是Session级别的缓存,它是属于事务范围的缓存.这一级别的缓存由hibernate管理的,一 ...

  8. 让powershell同时只能运行一个脚本(进程互斥例子)

    powershell,mutex,互斥,进程互斥,脚本互斥 powershell脚本互斥例子,在powershell类别文章中,声明原创唯一. powershell 传教士 原创文章 2016-07- ...

  9. fake gucci outlet perform a couple associated with things in great trust

    Based on my a lot of years of encounter within Taobao, purchase bags must go to the high reputation ...

  10. Longest Substring Without Repeating Characters (c#)

    Given a string, find the length of the longest substring without repeating characters. For example, ...