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. 1310. ACM Diagnostics

    http://acm.timus.ru/problem.aspx?space=1&num=1310 题目中说的 “the lexicographically increasing list” ...

  2. 更新证书错误:No matching provisioning profiles found

    在Xcode中当你在更新了你得证书而再重新编译你的程序,真机调试会出现“Your build settings specify a provisioning profile with the UUID ...

  3. 不经过 App store 的安装方式(转)

    所有安装到真机(非越狱)的应用(可以是 .app ,也可以是 .ipa ,只要编译时选的是编译成 Arm 的就好..app 转 .ipa 只需要一条命令) 都必须经过证书签名.证书主要有三大种: 企业 ...

  4. 使用wex5得到的一些教训

    博主一直都是做web开发,前段时间有个小想法,想给自己做个android小应用(很小,功能特别简单). 了解到可以用js直接做,貌似很简单,选用了wex5(基于codova插件)来直接开发. 最终发现 ...

  5. Javascript常用对象的属性和方法

    javascript为我们提供了一些非常有用的常用内部对象和方法.用户不需要用脚本来实现这些功能.这正是基于对象编程的真正目的. 在javascript提供了string(字符串).math(数值计算 ...

  6. alert弹层无法取消问题解决办法

    最近做H5移动端开发的时候,js代码写了个alert,在Android手机上能正常运行,但是在IOS上运行弹出之后却无法取消掉, 而且页面卡死,点不了任何东西,这种情况是非常不好的,用户体验非常糟糕. ...

  7. php常见的关键字

    一 instanceof 检测一个对象是否属于某个类型 <?php class A { } $a = new A(); if($a instanceof A) { echo "yes& ...

  8. [网络技术][转]路由表查找过程(ip_route_input_slow)

     若干解释: 判断in_dev是否存在,是通过mac地址吗? 源IP地址如果是multicast,broadcast,loopback地址,意味着数据报不知道从哪来的,只能把数据报废掉了. 目标IP地 ...

  9. Spark External Datasets

    Spark能够从任何支持Hadoop的存储源来创建RDD,包括本地的文件系统,HDFS,Cassandra,Hbase,Amazon S3等.Spark支持textFile.SequenceFiles ...

  10. NASAL脚本实现的高精度定时器

    #timer thread #-------以下:用户禁止访问------- #定时器属性 var TimerHash = { #定时间隔 time : , #触发函数 trigFunc : nil, ...