Remove characters that appear more than k times
Given a string of lowercase letters, reduce it by removing the characters which appear more than k times in the string.
Examples:
Input: str = "geeksforgeeks"
k = 2
Output: for
Input: str = "geeksforgeeks"
k = 3
Output: gksforgks
Approach :
- Create a hash table of 26 indexes, where the 0th index represents ‘a’ and 1th index represents ‘b’ and so on. Initialize the hash table to zero.
- Iterate through the string and count increment the frequency of the str[i] character in the hash table.
- Now once again traverse through the string and append those characters in the new string whose frequency in the hash table is less than k and skip those which appear more than equal to k.
Time Complexity: O(N)
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
void 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' ]++;
int index = 0;
for ( int i = 0; i < n; ++i) {
if (hash[arr[i] - 'a' ] < k) {
arr[index++] = arr[i];
}
}
arr[index] = '\0' ;
}
int main()
{
char str[] = "geeksforgeeks" ;
int k = 2;
removeChars(str, k);
cout << str;
return 0;
}
|
Java
import java.util.*;
class Solution
{
static final int MAX_CHAR = 26 ;
static void removeChars( char arr[], int k)
{
int hash[]= new int [MAX_CHAR];
for ( int i = 0 ; i <MAX_CHAR; ++i)
hash[i]= 0 ;
int n = (arr).length;
for ( int i = 0 ; i < n; ++i)
hash[arr[i] - 'a' ]++;
int index = 0 ;
for ( int i = 0 ; i < n; ++i) {
if (hash[arr[i] - 'a' ] < k) {
arr[index++] = arr[i];
}
}
for ( int i = index; i < n; ++i)
arr[i] = ' ' ;
}
public static void main(String args[])
{
char str[] = "geeksforgeeks" .toCharArray();;
int k = 2 ;
removeChars(str, k);
System.out.println(String.valueOf( str));
}
}
|
Python3
MAX_CHAR = 26
def removeChars(arr, k):
hash = [ 0 for i in range (MAX_CHAR)]
n = len (arr)
for i in range (n):
hash [ ord (arr[i]) - ord ( 'a' )] + = 1
index = 0
for i in range (n):
if ( hash [ ord (arr[i]) - ord ( 'a' )] < k):
arr[index] = arr[i]
index + = 1
arr[index] = ' '
for i in range (index):
print (arr[i], end = '')
if __name__ = = '__main__' :
str = "geeksforgeeks"
str = list ( str )
k = 2
removeChars( str , k)
|
C#
using System;
public class Solution{
static readonly int MAX_CHAR = 26;
static void removeChars( char []arr, int k)
{
int []hash= new int [MAX_CHAR];
for ( int i = 0; i <MAX_CHAR; ++i)
hash[i]=0;
int n = (arr).Length;
for ( int i = 0; i < n; ++i)
hash[arr[i] - 'a' ]++;
int index = 0;
for ( int i = 0; i < n; ++i) {
if (hash[arr[i] - 'a' ] < k) {
arr[index++] = arr[i];
}
}
for ( int i = index; i < n; ++i)
arr[i] = ' ' ;
}
public static void Main()
{
char []str = "geeksforgeeks" .ToCharArray();;
int k = 2;
removeChars(str, k);
Console.Write(String.Join( "" ,str));
}
}
|
PHP
<?php
$MAX_CHAR = 26;
function removeChars( $arr , $k )
{
global $MAX_CHAR ;
$hash = array_fill (0, $MAX_CHAR , NULL);
$n = strlen ( $arr );
for ( $i = 0; $i < $n ; ++ $i )
$hash [ord( $arr [ $i ]) - ord( 'a' )]++;
$index = 0;
for ( $i = 0; $i < $n ; ++ $i )
{
if ( $hash [ord( $arr [ $i ]) - ord( 'a' )] < $k )
{
$arr [ $index ++] = $arr [ $i ];
}
}
$arr [ $index ] = '' ;
for ( $i = 0; $i < $index ; $i ++)
echo $arr [ $i ];
}
$str = "geeksforgeeks" ;
$k = 2;
removeChars( $str , $k );
?>
|
Javascript
<script>
let MAX_CHAR = 26;
function removeChars(str,k)
{
let hash = new Array(MAX_CHAR);
for (let i=0;i<hash.length;i++)
{
hash[i]=0;
}
let n = str.length;
for (let i = 0; i < n; ++i) {
hash[str[i].charCodeAt(0) -
'a' .charCodeAt(0)]++;
}
let index = "" ;
for (let i = 0; i < n; ++i) {
if (hash[str[i].charCodeAt(0) -
'a' .charCodeAt(0)] < k)
{
index += str[i];
}
}
return index;
}
let str = "geeksforgeeks" ;
let k = 2;
document.write(removeChars(str, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), where n represents the size of the given array.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method #2:Using Built-in Python functions:
- We will scan the string and count the occurrence of all characters using built-in Counter() function.
- Now once again traverse through the string and append those characters in the new string whose frequency in the frequency dictionary is less than k and skip those which appear more than equal to k.
Note: This method is applicable for all types of characters.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
string removeChars(string str, int k)
{
unordered_map< char , int > freq;
for ( int i = 0; i < str.length(); i++) {
char c = str[i];
freq++;
}
string res = "" ;
for ( int i = 0; i < str.length(); i++) {
char c = str[i];
if (freq < k) {
res += c;
}
}
return res;
}
int main()
{
string str = "geeksforgeeks" ;
int k = 2;
cout << removeChars(str, k) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static String removeChars(String str, int k)
{
Map<Character, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < str.length(); i++) {
char c = str.charAt(i);
freq.put(c, freq.getOrDefault(c, 0 ) + 1 );
}
StringBuilder res = new StringBuilder();
for ( int i = 0 ; i < str.length(); i++) {
char c = str.charAt(i);
if (freq.get(c) < k) {
res.append(c);
}
}
return res.toString();
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int k = 2 ;
System.out.println(removeChars(str, k));
}
}
|
Python3
from collections import Counter
def removeChars( str , k):
freq = Counter( str )
res = ""
for i in range ( len ( str )):
if (freq[ str [i]] < k):
res + = str [i]
return res
if __name__ = = "__main__" :
str = "geeksforgeeks"
k = 2
print (removeChars( str , k))
|
C#
using System;
using System.Collections.Generic;
class Gfg
{
static string RemoveChars( string str, int k)
{
Dictionary< char , int > freq = new Dictionary< char , int >();
for ( int i = 0; i < str.Length; i++)
{
char c = str[i];
if (!freq.ContainsKey(c))
{
freq.Add(c, 0);
}
freq++;
}
string res = "" ;
for ( int i = 0; i < str.Length; i++)
{
char c = str[i];
if (freq < k)
{
res += c;
}
}
return res;
}
static void Main( string [] args)
{
string str = "geeksforgeeks" ;
int k = 2;
Console.WriteLine(RemoveChars(str, k));
}
}
|
Javascript
function removeChars(str, k)
{
let freq = {};
for (let i = 0; i < str.length; i++) {
freq[str[i]] = (freq[str[i]] || 0) + 1;
}
let res = "" ;
for (let i = 0; i < str.length; i++)
{
if (freq[str[i]] < k) {
res += str[i];
}
}
return res;
}
let str = "geeksforgeeks" ;
let k = 2;
console.log(removeChars(str, k));
|
Complexity Analysis:
- Time Complexity: O(n), where n represents the size of the given array.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...