diff --git a/1312_Minimum_Insertion_Steps_to_Make_a_String_Palindrome.cpp b/1312_Minimum_Insertion_Steps_to_Make_a_String_Palindrome.cpp new file mode 100644 index 0000000..f7e3a7d --- /dev/null +++ b/1312_Minimum_Insertion_Steps_to_Make_a_String_Palindrome.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int solve(int s, int e, string &str, vector> &dp){ + if(s > e || s == e) + return 0; + if(dp[s][e] != -1) return dp[s][e]; + + if(str[s] == str[e]) + return dp[s][e] = solve(s+1, e-1, str,dp); + else + return dp[s][e] = 1 + min(solve(s+1, e, str, dp), solve(s, e - 1, str,dp)); + } + int minInsertions(string s) { + int n = s.size(); + vector> dp(n, vector(n, -1)); + return solve(0, n-1, s, dp); + } +};