Created
September 18, 2017 22:27
-
-
Save cixuuz/d24a6ab3fa29c794a0020d70dee46dbc to your computer and use it in GitHub Desktop.
[132. Palindrome Partitioning II] #leetcode
This file contains hidden or 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
class Solution { | |
public int minCut(String s) { | |
char[] c = s.toCharArray(); | |
int n = c.length; | |
int[] cut = new int[n]; | |
boolean[][] pal = new boolean[n][n]; | |
for(int i = 0; i < n; i++) { | |
int min = i; | |
for(int j = 0; j <= i; j++) { | |
if(c[j] == c[i] && (j + 1 > i - 1 || pal[j + 1][i - 1])) { | |
pal[j][i] = true; | |
min = Math.min(min, j == 0 ? 0 : cut[j - 1] + 1); | |
} | |
} | |
cut[i] = min; | |
} | |
return cut[n - 1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment