From b0fbe2cffa52b34d6c43d93052e12c2bfe77eda2 Mon Sep 17 00:00:00 2001 From: RishiMohanJha1 <116079786+RishiMohanJha1@users.noreply.github.com> Date: Tue, 18 Oct 2022 17:16:06 +0530 Subject: [PATCH] Create 1312_Minimum_Insertion_Steps_to_Make_a_String_Palindrome.cpp --- ...rtion_Steps_to_Make_a_String_Palindrome.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1312_Minimum_Insertion_Steps_to_Make_a_String_Palindrome.cpp 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); + } +};