🎉 One-stop destination for all your technical interview Preparation 🎉
string size - length of longest palindromic subsequence.
int getLengthOfLCS(string& a, string& b)
{
int n = a.size(), m = b.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
}
}
return dp[n][m];
}
int longestPalindromeSubsequence(string s)
{
string t = s;
reverse(t.begin(),t.end());
return getLengthOfLCS(s,t);
}
int minInsertion(string &str)
{
return str.size()-longestPalindromeSubsequence(str);
}
``