From cccf0544199f1739c68e89b8fd1fc47ddcd3c572 Mon Sep 17 00:00:00 2001 From: MAINAK CHAUDHURI <64016811+MainakRepositor@users.noreply.github.com> Date: Sat, 5 Jun 2021 12:24:34 +0530 Subject: [PATCH] Create 483.cpp --- 483.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 483.cpp diff --git a/483.cpp b/483.cpp new file mode 100644 index 0000000..38855f7 --- /dev/null +++ b/483.cpp @@ -0,0 +1,32 @@ +// A Naive recursive program to find minimum +// number insertions needed to make a string +// palindrome +#include +using namespace std; + + +// Recursive function to find +// minimum number of insertions +int findMinInsertions(char str[], int l, int h) +{ + // Base Cases + if (l > h) return INT_MAX; + if (l == h) return 0; + if (l == h - 1) return (str[l] == str[h])? 0 : 1; + + // Check if the first and last characters are + // same. On the basis of the comparison result, + // decide which subrpoblem(s) to call + return (str[l] == str[h])? + findMinInsertions(str, l + 1, h - 1): + (min(findMinInsertions(str, l, h - 1), + findMinInsertions(str, l + 1, h)) + 1); +} + +// Driver code +int main() +{ + char str[] = "geeks"; + cout << findMinInsertions(str, 0, strlen(str) - 1); + return 0; +}