Print all characters of string whose frequency is a power of K
Last Updated :
13 Dec, 2021
Given string str of size N, the task is to print the characters of string whose frequency is a power of K in a lexicographically sorted order.
Examples:
Input: str = “aaacbb” K = 2
Output: bbc
Explanation: Frequency of a is 3 which is not the power of 2. Frequency of c is 1 and frequency of b is 2 which are the power of 2.
Input: str = “geeksgeekgeeks” K = 3
Output: eeeeeegggkkk
Naive Approach: The idea is to count frequency for every alphabet of the string, if the frequency is the power of K then add it to a new string. Sort the string and print it.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use Hashing. Below are the steps:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countFrequency(string str, int N, int K)
{
map< char , int > freq;
for ( int i = 0; i < N; i++) {
freq[str[i]]++;
}
for ( auto i : freq) {
int lg = log2(i.second);
int a = pow (2, lg);
if (a == i.second) {
while (a--)
cout << i.first << endl;
}
}
}
int main()
{
string str = "aaacbb" ;
int N = str.size();
int K = 2;
countFrequency(str, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countFrequency(String str, int N, int K)
{
HashMap<Character,
Integer> freq = new HashMap<Character,
Integer>();
for ( int i = 0 ; i < N; i++)
{
if (freq.containsKey(str.charAt(i)))
{
freq.put(str.charAt(i),
freq.get(str.charAt(i)) + 1 );
}
else
{
freq.put(str.charAt(i), 1 );
}
}
for (Map.Entry<Character, Integer> i : freq.entrySet())
{
int lg = ( int )Math.ceil(Math.log(i.getValue()));
int a = ( int )Math.pow( 2 , lg);
if (a == i.getValue())
{
while (a--> 0 )
System.out.print(i.getKey() + "\n" );
}
}
}
public static void main(String[] args)
{
String str = "aaacbb" ;
int N = str.length();
int K = 2 ;
countFrequency(str, N, K);
}
}
|
Python3
import math
def countFrequency( str , N, K):
freq = {}
for i in range (N):
if str [i] in freq.keys():
freq[ str [i]] = freq[ str [i]] + 1
else :
freq[ str [i]] = 1
for i in sorted (freq.keys()):
lg = math.floor(math.log2(freq[i]))
a = math. pow ( 2 , lg)
if a = = freq[i]:
while a ! = 0 :
print (i)
a = a - 1
str = "aaacbb"
N = len ( str )
K = 2
countFrequency( str , N, K)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static void countFrequency( string str, int N, int K)
{
Dictionary< char , int > freq =
new Dictionary< char , int >();
foreach ( char i in str)
{
if (freq.ContainsKey(i))
{
freq[i]++;
}
else
{
freq[i]=1;
}
}
ArrayList ch = new ArrayList();
foreach (KeyValuePair< char , int > i in freq)
{
int lg = ( int )Math.Ceiling(Math.Log(i.Value));
int a = ( int )Math.Pow(2, lg);
if (a == i.Value)
{
while (a-->0)
ch.Add(i.Key);
}
}
ch.Sort();
for ( int i = 0; i < ch.Count; i++){
Console.Write(ch[i] + "\n" );
}
}
public static void Main () {
string str = "aaacbb" ;
int N = str.Length;
int K = 2;
countFrequency(str, N, K);
}
}
|
Javascript
<script>
function countFrequency(str, N, K) {
let freq = new Map();
for (let i = 0; i < N; i++) {
if (freq.has(str[i])) {
freq.set(str[i], freq.get(str[i]) + 1)
} else {
freq.set(str[i], 1)
}
}
let ch = [];
for (i of freq) {
let lg = Math.floor(Math.log2(i[1]));
let a = Math.pow(2, lg);
if (a == i[1]) {
while (a--)
ch.push(i[0]);
}
}
ch.sort()
ch.forEach((val) => { document.write(val + "<br>" ) })
}
let str = "aaacbb" ;
let N = str.length;
let K = 2;
countFrequency(str, N, K);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...