# Minimum number of Appends needed to make a string palindrome

Given a string s we need to tell minimum characters to be appended (insertion at end) to make a string palindrome.

Examples:

```Input : s = "abede"
Output : 2
We can make string palindrome as "abedeba"
by adding ba at the end of the string.

Input : s = "aabb"
Output : 2
We can make string palindrome as"aabbaa"
by adding aa at the end of the string.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The solution can be achieved by removing characters from the beginning of the string one by one and checking if the string is palindrome or not.

For Example, consider the above string, s = “abede”.

We check if the string is palindrome or not.

The result is false, then we remove the character from the beginning of string and now string becomes “bede”.

We check if the string is palindrome or not. The result is again false, then we remove the character from the beginning of string and now string becomes “ede”.

We check if the string is palindrome or not. The result is true, so the output becomes 2 which is the number of characters removed from the string.

## C++

 `// C program to find minimum number of appends ` `// needed to make a string Palindrome ` `#include ` `#include ` `#include ` ` `  `// Checking if the string is palindrome or not ` `bool` `isPalindrome(``char` `*str) ` `{ ` `    ``int` `len = ``strlen``(str); ` ` `  `    ``// single character is always palindrome ` `    ``if` `(len == 1) ` `        ``return` `true``; ` ` `  `    ``// pointing to first character ` `    ``char` `*ptr1 = str; ` ` `  `    ``// pointing to last character ` `    ``char` `*ptr2 = str+len-1; ` ` `  `    ``while` `(ptr2 > ptr1) ` `    ``{ ` `        ``if` `(*ptr1 != *ptr2) ` `            ``return` `false``; ` `        ``ptr1++; ` `        ``ptr2--; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Recursive function to count number of appends ` `int` `noOfAppends(``char` `s[]) ` `{ ` `    ``if` `(isPalindrome(s)) ` `        ``return` `0; ` ` `  `    ``// Removing first character of string by ` `    ``// incrementing base address pointer. ` `    ``s++; ` ` `  `    ``return` `1 + noOfAppends(s); ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``char` `s[] = ``"abede"``; ` `    ``printf``(``"%d\n"``, noOfAppends(s)); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find minimum number of appends ` `// needed to make a string Palindrome ` `class` `GFG  ` `{ ` ` `  `// Checking if the string is palindrome or not ` `static` `boolean` `isPalindrome(``char` `[]str) ` `{ ` `    ``int` `len = str.length; ` ` `  `    ``// single character is always palindrome ` `    ``if` `(len == ``1``) ` `        ``return` `true``; ` ` `  `    ``// pointing to first character ` `    ``char` `ptr1 = str[``0``]; ` ` `  `    ``// pointing to last character ` `    ``char` `ptr2 = str[len-``1``]; ` ` `  `    ``while` `(ptr2 > ptr1) ` `    ``{ ` `        ``if` `(ptr1 != ptr2) ` `            ``return` `false``; ` `        ``ptr1++; ` `        ``ptr2--; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Recursive function to count number of appends ` `static` `int` `noOfAppends(String s) ` `{ ` `    ``if` `(isPalindrome(s.toCharArray())) ` `        ``return` `0``; ` ` `  `    ``// Removing first character of string by ` `    ``// incrementing base address pointer. ` `    ``s=s.substring(``1``); ` ` `  `    ``return` `1` `+ noOfAppends(s); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String arr[]) ` `{ ` `    ``String s = ``"abede"``; ` `    ``System.out.printf(``"%d\n"``, noOfAppends(s)); ` `} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## Python3

 `# Python3 program to find minimum number of appends ` `# needed to make a String Palindrome ` ` `  `# Checking if the String is palindrome or not ` `def` `isPalindrome(``Str``): ` ` `  `    ``Len` `=` `len``(``Str``) ` ` `  `    ``# single character is always palindrome ` `    ``if` `(``Len` `=``=` `1``): ` `        ``return` `True` ` `  `    ``# pointing to first character ` `    ``ptr1 ``=` `0` ` `  `    ``# pointing to last character ` `    ``ptr2 ``=` `Len` `-` `1` ` `  `    ``while` `(ptr2 > ptr1): ` ` `  `        ``if` `(``Str``[ptr1] !``=` `Str``[ptr2]): ` `            ``return` `False` `        ``ptr1 ``+``=` `1` `        ``ptr2 ``-``=` `1` ` `  `    ``return` `True` ` `  `# Recursive function to count number of appends ` `def` `noOfAppends(s): ` ` `  `    ``if` `(isPalindrome(s)): ` `        ``return` `0` ` `  `    ``# Removing first character of String by ` `    ``# incrementing base address pointer. ` `    ``del` `s[``0``] ` ` `  `    ``return` `1` `+` `noOfAppends(s) ` ` `  `# Driver Code ` `se ``=` `"abede"` `s ``=` `[i ``for` `i ``in` `se] ` `print``(noOfAppends(s)) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# program to find minimum number of appends  ` `// needed to make a string Palindrome  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `// Checking if the string is palindrome or not  ` `static` `Boolean isPalindrome(``char` `[]str)  ` `{  ` `    ``int` `len = str.Length;  ` ` `  `    ``// single character is always palindrome  ` `    ``if` `(len == 1)  ` `        ``return` `true``;  ` ` `  `    ``// pointing to first character  ` `    ``char` `ptr1 = str;  ` ` `  `    ``// pointing to last character  ` `    ``char` `ptr2 = str[len-1];  ` ` `  `    ``while` `(ptr2 > ptr1)  ` `    ``{  ` `        ``if` `(ptr1 != ptr2)  ` `            ``return` `false``;  ` `        ``ptr1++;  ` `        ``ptr2--;  ` `    ``}  ` ` `  `    ``return` `true``;  ` `}  ` ` `  `// Recursive function to count number of appends  ` `static` `int` `noOfAppends(String s)  ` `{  ` `    ``if` `(isPalindrome(s.ToCharArray()))  ` `        ``return` `0;  ` ` `  `    ``// Removing first character of string by  ` `    ``// incrementing base address pointer.  ` `    ``s=s.Substring(1);  ` ` `  `    ``return` `1 + noOfAppends(s);  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String []arr)  ` `{  ` `    ``String s = ``"abede"``;  ` `    ``Console.Write(``"{0}\n"``, noOfAppends(s));  ` `}  ` `}  ` ` `  `// This code has been contributed by 29AjayKumar `

Output:

```2
```

This article is contributed by Shubham Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

9

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.