LintCode Palindrome Partitioning II
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的更多相关文章
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
		Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ... 
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
		LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ... 
- 【leetcode】Palindrome Partitioning II
		Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ... 
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
		https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ... 
- [LeetCode] Palindrome Partitioning II 解题笔记
		Given a string s, partition s such that every substring of the partition is a palindrome. Return the ... 
- 动态规划——Palindrome Partitioning II
		Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ... 
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
		131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ... 
- LeetCode: Palindrome Partitioning II  解题报告
		Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ... 
- 【LeetCode】132. Palindrome Partitioning II
		Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ... 
随机推荐
- 关于ssh上传文件
			今天用ssh传项目到公司总部的服务器上,报了错误: encountered 1 errors during the transfer 重启ssh再次上传还是一样的错误,然后我让公司那里重启一下服务器, ... 
- 修改UISearchBar的背景颜色
			当你看到这篇博客你就已经发现了用_searchBar.backgroundColor = [UIColor clearColor];来设置UISearchBar的颜色完全没有效果: 并且,有些方法是想 ... 
- Bootstrap学习笔记(一)
			一.什么是Bootstrap bootstrap是一款css框架,便于响应式设计. 二.怎样使用bootstarp 最常用的方法,在html结构中引入样式表bootstarp.min.css,以及jq ... 
- Primitive JS completion of AJAX
			Firstly , let us explain XMLHttpRequest open(), send(), readyState 1. open(method, url, async, user, ... 
- 【墙内备份】Android 6.0 APIs
			Android 6.0 APIs In this documentSHOW MORE Fingerprint Authentication Confirm Credential App Linking ... 
- 转 从腾讯那“偷 了”3000万QQ用户数据
			http://www.icaijing.com/hot/article4899809/ http://news.cnblogs.com/n/533061/ 
- Tomcat Shell脚本(启动|关闭|重启|状态)
			#!/bin/bash # # chkconfig: - # description: Tomcat start/stop/status script #Location of JAVA_HOME ( ... 
- [javascript|基本概念|Number
			Number类型的值:整数/浮点数值 整数 十进制 e.g.: var intNum = 50; 八进制 (严格模式下无效,解析错误)字面值首位必须是0,之后的数字序列为0-7 e.g.: va ... 
- Mvc api HelpPage 与注释
			一.添加包Microsoft.AspNet.WebApi.HelpPage可以自动给api生成帮助页面,url:/help 二.help加注释: 1. 2. public static class H ... 
- 自定义jsp标签
			1.类: package ===============================; import javax.servlet.jsp.JspTagException; import javax ... 
