palindrome-partitioning-ii leetcode C++

mac2022-06-30  115

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.

For example, given s ="aab", Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.

C++

class Solution { public: int minCut(string s) { int n = s.size(); if(n < 2) return 0; vector<int> dp(n, INT_MAX); vector<bool> tmp(n, false); vector<vector<bool> > p(n, tmp); for(int i=0; i<n; i++){ for(int j=i; j>-1; j--){ if(s[i] == s[j] && (i-j < 2 || p[j+1][i-1])){ p[j][i] = true; dp[i] = min(dp[i], j == 0? 0 : dp[j-1] + 1); } } } return dp.back(); } };

 

转载于:https://www.cnblogs.com/vercont/p/10210270.html

最新回复(0)