classSolution{public:intminCut(strings){constintn=s.length();// isPalindrome[i][j] := true if s[i..j] is a palindromevector<vector<bool>>isPalindrome(n,vector<bool>(n,true));// dp[i] := min cuts needed for a palindrome partitioning of s[0..i]vector<int>dp(n,n);for(intl=2;l<=n;++l)for(inti=0,j=l-1;j<n;++i,++j)isPalindrome[i][j]=s[i]==s[j]&&isPalindrome[i+1][j-1];for(inti=0;i<n;++i){if(isPalindrome[0][i]){dp[i]=0;continue;}// Try all possible partitionsfor(intj=0;j<i;++j)if(isPalindrome[j+1][i])dp[i]=min(dp[i],dp[j]+1);}returndp.back();}};
classSolution{publicintminCut(Strings){finalintn=s.length();// isPalindrome[i][j] := true if s[i..j] is a palindromeboolean[][]isPalindrome=newboolean[n][n];for(boolean[]row:isPalindrome)Arrays.fill(row,true);// dp[i] := min cuts needed for a palindrome partitioning of s[0..i]int[]dp=newint[n];Arrays.fill(dp,n);for(intl=2;l<=n;++l)for(inti=0,j=l-1;j<n;++i,++j)isPalindrome[i][j]=s.charAt(i)==s.charAt(j)&&isPalindrome[i+1][j-1];for(inti=0;i<n;++i){if(isPalindrome[0][i]){dp[i]=0;continue;}// Try all possible partitionsfor(intj=0;j<i;++j)if(isPalindrome[j+1][i])dp[i]=Math.min(dp[i],dp[j]+1);}returndp[n-1];}}