Problem
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
Example:
Input: "aab" Output: 1 Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
Code
public class Solution {
public int minCut(String s) {
int ls = s.length();
int[] ans = new int[ls];
List<Integer> arr = new LinkedList();
for (int i = 0; i < ls; ++i) {
arr.add(0, i);
if (i == 0) {
ans[i] = 0;
continue;
}
ans[i] = ans[i - 1] + 1;
List<Integer> tmp = new LinkedList<Integer>();
tmp.add(i);
for (int a : arr) {
if (a != 0) {
if (s.charAt(a - 1) == s.charAt(i)) {
tmp.add(a - 1);
if (a - 1 == 0) {
ans[i] = 0;
} else {
if (ans[a - 2] + 1 < ans[i]) {
ans[i] = ans[a - 2] + 1;
}
}
}
}
}
arr = tmp;
}
return ans[ls - 1];
}
}