Given a string str and an integer K, the task is to check if a permutation of the given string can be made a palindromic by removing at most K characters from the given string.
Examples:
Input: str = “geeksforgeeks”, K = 2
Output: Yes
Explanation:
Removing (str[5], str[6]) from the given string makes the remaining string “geeksrgeeks” palindromic. Therefore, the required output is Yes.
Input: str = “coder”, K = 1
Output: No
Approach: The problem can be solved using Hashing. The idea is to iterate over characters of the given string and store the frequency of each distinct character of the given string. If the count of distinct characters of the given string having odd frequency is less than or equal to (K + 1), then print Yes. Otherwise, print No. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPalinK(string str, int K)
{
int N = str.length();
int cntFreq[256] = { 0 };
for ( int i = 0; i < N;
i++) {
cntFreq[str[i]]++;
}
int cntOddFreq = 0;
for ( int i = 0; i < 256;
i++) {
if (cntFreq[i] % 2
== 1) {
cntOddFreq++;
}
}
if (cntOddFreq <= (K + 1)) {
return true ;
}
return false ;
}
int main()
{
string str = "geeksforgeeks" ;
int K = 2;
if (checkPalinK(str, K)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
|
Java
import java.util.*;
class GFG{
public static boolean checkPalinK(String str,
int K)
{
int N = str.length();
int cntFreq[] = new int [ 256 ];
for ( int i = 0 ; i < N; i++)
{
cntFreq[str.charAt(i)]++;
}
int cntOddFreq = 0 ;
for ( int i = 0 ; i < 256 ; i++)
{
if (cntFreq[i] % 2 == 1 )
{
cntOddFreq++;
}
}
if (cntOddFreq <= (K + 1 ))
{
return true ;
}
return false ;
}
public static void main(String args[])
{
String str = "geeksforgeeks" ;
int K = 2 ;
if (checkPalinK(str, K))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def checkPalinK( str , K):
N = len ( str )
cntFreq = [ 0 ] * 256
for i in range (N):
cntFreq[ ord ( str [i])] + = 1
cntOddFreq = 0
for i in range ( 256 ):
if (cntFreq[i] % 2 = = 1 ):
cntOddFreq + = 1
if (cntOddFreq < = (K + 1 )):
return True
return False
if __name__ = = '__main__' :
str = "geeksforgeeks"
K = 2
if (checkPalinK( str , K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static bool checkPalinK(String str,
int K)
{
int N = str.Length;
int []cntFreq = new int [256];
for ( int i = 0; i < N; i++)
{
cntFreq[str[i]]++;
}
int cntOddFreq = 0;
for ( int i = 0; i < 256; i++)
{
if (cntFreq[i] % 2 == 1)
{
cntOddFreq++;
}
}
if (cntOddFreq <= (K + 1))
{
return true ;
}
return false ;
}
public static void Main(String []args)
{
String str = "geeksforgeeks" ;
int K = 2;
if (checkPalinK(str, K))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function checkPalinK(str, K)
{
var N = str.length;
var cntFreq = Array(256).fill(0);
var i;
for (i = 0; i < N;
i++) {
cntFreq[str[i]] += 1;
}
var cntOddFreq = 0;
for (i = 0; i < 256;
i++) {
if (cntFreq[i] % 2
== 1) {
cntOddFreq++;
}
}
if (cntOddFreq <= (K + 1)) {
return true ;
}
return false ;
}
var str = "geeksforgeeks" ;
var K = 2;
if (checkPalinK(str, K)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(N + 256), Where N is the length of the given string.
Auxiliary Space: O(256)
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 geeks!