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++
#include <bits/stdc++.h>
using namespace std;
bool isPossible(string str, int n)
{
int len = str.length();
if (len >= n)
return true ;
return false ;
}
int main()
{
string str = "abccba" ;
int n = 4;
if (isPossible(str, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isPossible(String str, int n)
{
int len = str.length();
if (len >= n)
return true ;
return false ;
}
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" );
}
}
|
Python
def isPossible( str , n):
l = len ( str )
if (l > = n):
return True
return False
str = "abccba"
n = 4
if (isPossible( str , n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isPossible(String str, int n)
{
int len = str.Length;
if (len >= n)
return true ;
return false ;
}
public static void Main(String[] args)
{
String str = "abccba" ;
int n = 4;
if (isPossible(str, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPossible(str, n)
{
var len = str.length;
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>
|
Time Complexity: O(1)
Auxiliary Space: O(1)