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 ...
随机推荐
- 【复位】FGPA的复位 [部分转]
关于FGPA的复位 当初开始学FPGA的时候,总是疑惑:FPGA不是没有复位管教么,但总在always看到有复位信号.这个复位信号(我们暂且称为rst_n)从哪里来? 实际上是可以从两个方面获得的,这 ...
- 也谈自动化平台的搭建,另附高大上的名字---无人值守定时巡检系统(selenium+testng+ant+jenkins)
最近公司新项目改版,由于没有运维,开发则负责上线任务,并且都是手动上线,并行的项目多了,分支混乱,经常导致B项目上线覆盖A项目,导致系统不定时出现异常,老板知道了便扣了大家的绩效,作为测试这边必须想个 ...
- imx6 gpio irq
/***************************************************************** * gpio irq * * 一直以来都没了解过gpio的irq, ...
- (翻译)Emacs Hooks
Table of Contents 1. 51.2.2 Hooks 51.2.2 Hooks Hooks(钩子或挂钩,为了保持文章的纯正性,这种专有名词不做翻译,后续以hooks为主),是定制化Ema ...
- JAVA开发相关
JAVA开发相关1. IntelliJ IDEA开发工具熟练使用2. Maven3. Spring框架(IoC.AOP) 1)数据库相关MyBatis 2)数据库连接池 3)事务.多数据源.跨数据库分 ...
- DateTime 详解
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- poj3693
//Accepted 12004 KB 407 ms /* source:poj3693 time :20150819 by :songt */ /*题解: 搞了一天,总算弄完了 首先,我们来明确一个 ...
- entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等
前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...
- JSP自定义标签——调用数据库(通过id号搜索相关信息)
一.创建新表(假设在master数据库下新建) 二.连接数据库 开始-->控制面板-->管理工具-->数据源-->系统DSN-->添加-->SQL Server-- ...
- RABBITMQ(小总结 持续更新...
(一)理解消息通信 1.消息通信概念---消费者.生产者和代理 生产者(producer)创建消息,然后发送到代理服务器(RaabitMQ). 其中消息包括两部分内容:有效载荷(payload)和标签 ...