Remove characters from a String that appears exactly K times
Given a string of lowercase letters str of length N, the task is to reduce it by removing the characters which appear exactly K times in the string.
Examples:
Input: str = “geeksforgeeks”, K = 2
Output: eeforee
Input: str = “geeksforgeeks”, K = 4
Output: gksforgks
Approach:
- Create a hash table of size 26, where 0th index represents ‘a’ and 1st index represent ‘b’ and so on.
- Initialize the hash table to zero.
- Iterate through the string and increment the frequency of each character(s[i]) in the hash table
- Now, once again traverse through the string and append the characters, with frequency K, in the new string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
string removeChars( char arr[], int k)
{
int hash[MAX_CHAR] = { 0 };
int n = strlen (arr);
for ( int i = 0; i < n; ++i)
hash[arr[i] - 'a' ]++;
string ans = "" ;
int index = 0;
for ( int i = 0; i < n; ++i) {
if (hash[arr[i] - 'a' ] != k) {
ans += arr[i];
}
}
return ans;
}
int main()
{
char str[] = "geeksforgeeks" ;
int k = 2;
cout << removeChars(str, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAX_CHAR = 26 ;
static String removeChars( char arr[], int k)
{
int []hash = new int [MAX_CHAR];
int n = arr.length;
for ( int i = 0 ; i < n; ++i)
hash[arr[i] - 'a' ]++;
String ans = "" ;
for ( int i = 0 ; i < n; ++i) {
if (hash[arr[i] - 'a' ] != k) {
ans += arr[i];
}
}
return ans;
}
public static void main(String[] args)
{
char str[] = "geeksforgeeks" .toCharArray();
int k = 2 ;
System.out.print(removeChars(str, k));
}
}
|
Python3
MAX_CHAR = 26
def removeChars(arr, k):
hash = [ 0 ] * MAX_CHAR
n = len (arr)
for i in range ( n):
hash [ ord (arr[i]) - ord ( 'a' )] + = 1
ans = ""
index = 0
for i in range (n):
if ( hash [ ord (arr[i]) - ord ( 'a' )] ! = k):
ans + = arr[i]
return ans
if __name__ = = "__main__" :
str = "geeksforgeeks"
k = 2
print (removeChars( str , k))
|
C#
using System;
class GFG{
static int MAX_CHAR = 26;
static String removeChars( char []arr, int k)
{
int []hash = new int [MAX_CHAR];
int n = arr.Length;
for ( int i = 0; i < n; ++i)
hash[arr[i] - 'a' ]++;
String ans = "" ;
for ( int i = 0; i < n; ++i) {
if (hash[arr[i] - 'a' ] != k) {
ans += arr[i];
}
}
return ans;
}
public static void Main(String[] args)
{
char []str = "geeksforgeeks" .ToCharArray();
int k = 2;
Console.Write(removeChars(str, k));
}
}
|
Javascript
<script>
let MAX_CHAR = 26;
function removeChars(arr, k)
{
let hash = Array.from({length: MAX_CHAR}, (_, i) => 0);
let n = arr.length;
for (let i = 0; i < n; ++i)
hash[arr[i].charCodeAt() - 'a' .charCodeAt()]++;
let ans = "" ;
for (let i = 0; i < n; ++i) {
if (hash[arr[i].charCodeAt() - 'a' .charCodeAt()] != k) {
ans += arr[i];
}
}
return ans;
}
let str = "geeksforgeeks" .split( '' );
let k = 2;
document.write(removeChars(str, k));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...