# Check if string remains palindrome after removing given number of characters

Given a palindromic string str and an integer N. The task is to find if it is possible to remove exactly N characters from the given string such that the string remains a palindrome.

Examples:

Input: str = “abba”, N = 1
Output: Yes
Remove ‘b’ and the reamianing string
“aba” is still a palindrome.

Input: str = “aba”, N = 1
Output: Yes

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

Approach: It can be observed that it is always possible to remove any number of characters less than or equal to its length from a palindromic string such that the resultant string remains a palindromic string.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if str ` `// remains a palindrome after removing ` `// exactly N characters from it ` `bool` `isPossible(string str, ``int` `n) ` `{ ` `    ``// Find the length of the string ` `    ``int` `len = str.length(); ` ` `  `    ``// If it is possible ` `    ``if` `(len >= n) ` `        ``return` `true``; ` ` `  `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"abccba"``; ` `    ``int` `n = 4; ` ` `  `    ``if` `(isPossible(str, n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG  ` `{ ` `     `  `    ``// Function that returns true if str ` `    ``// remains a palindrome after removing ` `    ``// exactly N characters from it ` `    ``static` `boolean` `isPossible(String str, ``int` `n) ` `    ``{ ` `        ``// Find the length of the string ` `        ``int` `len = str.length(); ` `     `  `        ``// If it is possible ` `        ``if` `(len >= n) ` `            ``return` `true``; ` `     `  `        ``return` `false``; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``String str = ``"abccba"``; ` `        ``int` `n = ``4``; ` `     `  `        ``if` `(isPossible(str, n)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python

 `# Python3 implementation of the approach ` ` `  `# Function that returns true if str  ` `# remains a palindrome after removing  ` `# exactly N characters from it ` `def` `isPossible(``str``, n): ` `     `  `    ``# Find the length of the string ` `    ``l ``=` `len``(``str``) ` ` `  `    ``# If it is possible ` `    ``if` `(l >``=` `n): ` `        ``return` `True` `     `  `    ``return` `False` ` `  `# Driver code ` `str` `=` `"abccba"` `n ``=` `4` ` `  `if``(isPossible(``str``, n)): ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function that returns true if str ` `    ``// remains a palindrome after removing ` `    ``// exactly N characters from it ` `    ``static` `bool` `isPossible(String str, ``int` `n) ` `    ``{ ` `        ``// Find the length of the string ` `        ``int` `len = str.Length; ` `     `  `        ``// If it is possible ` `        ``if` `(len >= n) ` `            ``return` `true``; ` `     `  `        ``return` `false``; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``String str = ``"abccba"``; ` `        ``int` `n = 4; ` `     `  `        ``if` `(isPossible(str, n)) ` `            ``Console.WriteLine(``"Yes"``); ` `        ``else` `            ``Console.WriteLine(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```Yes
```

Time Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01, princiraj1992

Article Tags :
Practice Tags :

Be the First to upvote.

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