Advertisement
Singasking

Untitled

Jan 14th, 2023
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int isPallindrome(string a) {
  4.        
  5.         int e = a.length()-1;
  6.         int s=0;
  7.         while(s<=e) {
  8.             if(a[s]==a[e]) { s++; e--; } else { return false;}
  9.         }
  10.         return true;
  11.     }
  12.     int f(int i,int j,string s,vector<vector<int>> &dp) {
  13.  
  14.       if(i>=j) {  return 0; }
  15.       if(isPallindrome(s))   { return 0; }
  16.    
  17.       if(dp[i][j]!=-1) return dp[i][j];
  18.        
  19.         int minI = INT_MAX;  
  20.  
  21.         for(int k=i;k<j;k++) {
  22.             int pCount = 1 + f(i,k,s,dp) + f(k+1,j,s,dp);
  23.             minI = min(pCount,minI);
  24.              // cout<<"mini:"<<minI<<endl;
  25.         }
  26.      
  27.         return  dp[i][j]=minI;
  28.         //Only 1 char-> is pallindrome}
  29.     }
  30.     int minCut(string s) {
  31.         vector<vector<int>> dp(s.length()+1,vector<int>(s.length()+1,-1));
  32.       return f(0,s.length()-1,s,dp);
  33.     }
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement