Created
March 14, 2013 00:06
-
-
Save guolinaileen/5157690 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int minCut(String s) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(s.length()==0) return 0; | |
int [] dp=new int [s.length()+1]; | |
for(int i=s.length(); i>=0; i--) | |
{ | |
dp[i]=s.length()-i; | |
} | |
boolean [][]palindrome=new boolean [s.length()][s.length()]; | |
for(int i=s.length()-1; i>=0 ; i--) | |
{ | |
for(int j=i; j<s.length(); j++) | |
{ | |
if(s.charAt(i)==s.charAt(j) && (j-i<2|| palindrome[i+1][j-1])) | |
{ | |
palindrome[i][j]=true; | |
dp[i]=Math.min(dp[i], 1+dp[j+1]); | |
} | |
} | |
} | |
return dp[0]-1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment