# Modify String by Sorting Characters after Removal of Characters Whose Frequency is not Equal to Power of 2

Modifying string by sorting characters after the removal of characters whose frequency is not equal to the power of 2 is a popular problem in the field of computer programming, particularly in the context of competitive programming. The problem involves taking a string as input and modifying it by removing characters whose frequency is not a power of 2, and then sorting the remaining characters in lexicographically increasing order.

In this tutorial, we will provide a detailed solution to this problem using the C++ programming language. We will start by discussing the problem statement in greater detail, exploring the various intricacies involved in solving it, and then provide a step-by-step guide on how to implement a solution using C++. We will also include code snippets and examples to help illustrate the concepts covered. So let's get started!

## Problem Statement

Given an array 'arr[]' consisting of 'N' strings, the task is to sort the array in ascending order after modifying each string by removing all characters that are not perfect power of 2 and then sort the modified string in descending order.

### Sample Examples

Example 1

For example, the input array 'arr[] = {“aaacbb”, “bleek”, “aaa”}' is modified as follows:

• The string "aaacbb" has 'a' with a frequency that is not a power of 2. Therefore, 'a' is removed from the string, resulting in "cbb". The modified string "cbb" is sorted in increasing order of frequency, which is the same as the original string. Therefore, "cbb" remains the same.

• The string "bleek" has all characters with a frequency that is a power of 2. Therefore, no characters are removed, and the string is sorted in increasing order of frequency, resulting in "lkeeb ".

• The string "aaa" has 'a' with a frequency that is not a power of 2. Therefore, 'a' is removed from the string, resulting in an empty string "".

Finally, the modified and sorted strings are concatenated together, resulting in the output "cbb lkeeb ".

Note that the program assumes that the input strings consist only of lowercase English alphabets.

Example 2

For example, if the input array is 'S[] = {“c”, “a”, “b”}', the program sorts it in alphabetical order, resulting in the output array 'S[] = {“a”, “b”, “c”}'.

### Algorithm

The given problem can be solved using Hashing to store the frequencies of characters in each string and then perform the specified operations. Follow the steps below to solve the problem:

• Iterate through the input array of strings, arr[], and for each string perform the following operations:

• Store the frequency of each character in a Map.

• Create an empty string, T, to store the modified string.

• Traverse the Map and append the characters whose frequency is a power of 2 to the string T.

• Sort the string T in ascending order of frequency and add it to an array of strings, res[].

• Sort the array res[] in increasing order.

• After completing the above steps, print the strings in the array res[] as the result.

Now, let’s understand the implementation of the above algorithm using C++ with the help of an example. So let’s do it!

### Example

Implementation of the above algorithm using C++

The program first stores the frequency of each character for every string of the input array in a hash table. It then modifies each string by keeping only the characters whose frequency is a power of 2 and sorts the modified strings in descending order. Finally, it sorts the modified strings in ascending order and prints the result.

The time complexity of the program is O(NMlog(M)), where N is the size of the input array, and M is the length of the longest string in the input array. This is because, for every string, we traverse it once to calculate the frequency of each character and then once again to modify the string. The sorting operation takes O(Mlog(M)) time complexity, and we do it for every string. Additionally, we also sort the modified strings at the end, which takes O(NM*log(M)) time complexity.

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPowerOfTwo(int n) {
if (n == 0) {
return false;
}
return (ceil(log2(n)) == floor(log2(n)));
}
void printArray(vector<string> res) {
sort(res.begin(), res.end());
cout << "Modified array= {";
for (int i = 0; i < res.size(); i++) {
cout << "\"" << res[i] << "\"";
if (i != res.size() - 1) {
cout << ", ";
}
}
cout << "}" << endl;
}
void sortedStrings(string S[], int N) {
unordered_map<char, int> freq;
vector<string> res;
for (int i = 0; i < N; i++) {
string st = "";
for (int j = 0; j < S[i].size(); j++) {
freq[S[i][j]]++;
}
for (auto i : freq) {
if (isPowerOfTwo(i.second)) {
for (int j = 0; j < i.second; j++) {
st += i.first;
}
}
}
freq.clear();
if (st.size() == 0) {
continue;
}
sort(st.begin(), st.end(), greater<char>());
res.push_back(st);
}
printArray(res);
}
int main() {
string arr[] = { "aaacbb", "bleek", "aaa" };
int N = sizeof(arr) / sizeof(arr[0]);
cout << "Input array= {";
for (int i = 0; i < N; i++) {
cout << "\"" << arr[i] << "\"";
if (i != N - 1) {
cout << ", ";
}
}
cout << "}" << endl;
sortedStrings(arr, N);
return 0;
}


### Output

Input array= {"aaacbb", "bleek", "aaa"}
Modified array= {"cbb", "lkeeb"}


## Conclusion

To sum up, the problem of modifying and sorting an array of strings based on character frequency can be efficiently solved using hashing technique. The time complexity of the solution is O(NM log M), where N is the number of strings and M is the length of the longest string in the array.

Updated on: 08-Sep-2023

18 Views