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 remaining string
“aba” is still a palindrome.Input: str = “aba”, N = 1
Output: Yes
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 <bits/stdc++.h>using namespace std;// Function that returns true if str// remains a palindrome after removing// exactly N characters from itbool 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 codeint main(){ string str = "abccba"; int n = 4; if (isPossible(str, n)) cout << "Yes"; else cout << "No"; return 0;} |
Java
// Java implementation of the approachclass 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 itdef isPossible(str, n): # Find the length of the string l = len(str) # If it is possible if (l >= n): return True return False# Driver codestr = "abccba"n = 4if(isPossible(str, n)): print("Yes")else: print("No") |
C#
// C# implementation of the approachusing 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 |
Javascript
<script>// Function that returns true if str// remains a palindrome after removing// exactly N characters from itfunction isPossible(str, n){ // Find the length of the string var len = str.length; // If it is possible if (len >= n) return true; return false;}var str = "abccba"; var n = 4; if (isPossible(str, n)) document.write("Yes"); else document.write("No");</script> |
Output:
Yes
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



