knakul853

Untitled

Jul 22nd, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minInsertions(string s) {
  4.        
  5.         int n = (int)s.size();
  6.          
  7.         vector<vector<int>>dp(n, vector<int>(n));
  8.        
  9. // dp[i][j] : what is minimum no of insertion to make palindrome that starts with i and ends at j.
  10.        
  11.         for(int i=n-2;i>=0;i--){
  12.            
  13.             for(int j=i+1; j<n; j++){
  14.                
  15.                 if(s[i] == s[j]){
  16.                     dp[i][j] = dp[i+1][j-1];
  17.                 }
  18.                 else{
  19.                     dp[i][j] = 1+min(dp[i][j-1], dp[i+1][j]);
  20.                 }
  21.             }
  22.            
  23.         }
  24.        
  25.        // cout<<dp[0][n-1]<<"\n";
  26.         return dp[0][n-1];
  27.     }
  28. };
Add Comment
Please, Sign In to add comment