Given a string S consisting of N lowercase alphabets, the task is to remove the characters from the string whose frequency is not a power of 2 and then sort the string in ascending order.
Examples:
Input: S = “aaacbb”
Output: bbc
Explanation: The frequencies of ‘a’, ‘b’, and ‘c’ in the given string are 3, 2, 1. Only the character ‘a’ has frequency (= 3) which is not a power of 2. Therefore, removing ‘a’ from the string S modifies it to “cbb”. Therefore, the modified string after sorting is “bbc”.
Input: S = “geeksforgeeks”
Output: eeeefggkkorss
Approach: The given problem can be solved by using Hashing. Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countFrequency(string S, int N)
{
int freq[26] = { 0 };
for ( int i = 0; i < N; i++) {
freq[S[i] - 'a' ]++;
}
for ( int i = 0; i < 26; i++) {
if (freq[i] == 0)
continue ;
int lg = log2(freq[i]);
int a = pow (2, lg);
if (a == freq[i]) {
while (freq[i]--)
cout << ( char )(i + 'a' );
}
}
}
int main()
{
string S = "aaacbb" ;
int N = S.size();
countFrequency(S, N);
return 0;
}
|
Java
class GFG{
static void countFrequency(String S, int N)
{
int []freq = new int [ 26 ];
for ( int i = 0 ; i < N; i++)
{
freq[( int )S.charAt(i) - 'a' ] += 1 ;
}
for ( int i = 0 ; i < 26 ; i++)
{
if (freq[i] == 0 )
continue ;
int lg = ( int )(Math.log(freq[i]) / Math.log( 2 ));
int a = ( int )Math.pow( 2 , lg);
if (a == freq[i])
{
while (freq[i] > 0 )
{
freq[i] -= 1 ;
System.out.print(( char )(i + 'a' ));
}
}
}
}
public static void main(String args[])
{
String S = "aaacbb" ;
int N = S.length();
countFrequency(S, N);
}
}
|
Python3
from math import log2
def countFrequency(S, N):
freq = [ 0 ] * 26
for i in range (N):
freq[ ord (S[i]) - ord ( 'a' )] + = 1
for i in range ( 26 ):
if (freq[i] = = 0 ):
continue
lg = int (log2(freq[i]))
a = pow ( 2 , lg)
if (a = = freq[i]):
while (freq[i]):
print ( chr (i + ord ( 'a' )), end = "")
freq[i] - = 1
if __name__ = = '__main__' :
S = "aaacbb"
N = len (S)
countFrequency(S, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countFrequency( string S, int N)
{
int []freq = new int [26];
Array.Clear(freq, 0, freq.Length);
for ( int i = 0; i < N; i++)
{
freq[( int )S[i] - 'a' ] += 1;
}
for ( int i = 0; i < 26; i++)
{
if (freq[i] == 0)
continue ;
int lg = ( int )Math.Log(( double )freq[i], 2.0);
int a = ( int )Math.Pow(2, lg);
if (a == freq[i])
{
while (freq[i] > 0)
{
freq[i] -= 1;
Console.Write(( char )(i + 'a' ));
}
}
}
}
public static void Main()
{
string S = "aaacbb" ;
int N = S.Length;
countFrequency(S, N);
}
}
|
Javascript
<script>
function countFrequency(S, N)
{
var freq = new Array(26).fill(0);
for ( var i = 0; i < N; i++)
{
freq[S[i].charCodeAt(0) - "a" .charCodeAt(0)]++;
}
for ( var i = 0; i < 26; i++)
{
if (freq[i] === 0) continue ;
var lg = parseInt(Math.log2(freq[i]));
var a = Math.pow(2, lg);
if (a === freq[i])
{
while (freq[i]--)
document.write(String.fromCharCode(i + "a" .charCodeAt(0)));
}
}
}
var S = "aaacbb" ;
var N = S.length;
countFrequency(S, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)