Sort array of strings after sorting each string after removing characters whose frequencies are not a powers of 2


In this problem, we need to remove the characters from the string whose frequency is not the power of 2. After that, we need to sort each string of the array in non-increasing order.

Problem statement- We have given an array arr[] containing total N strings of different lengths. We need to remove the character from the string if its frequency is not the power of 2. After that, we need to sort each string

Sample examples

Input – arr[] = {"abde", "cpcc", "ddddd", "kk"}

Output – edba, p, kk

Explanation

  • In the string ‘abde’ string, all characters' frequency is 1, equal to 2^0.

  • In the string ‘cpcc’, the frequency of ‘c’ is 3, which is not equal to any power of 2. So, we have removed that.

  • In the string ‘ddddd’ frequency of ‘d’ is 5. So, all characters are removed from the string.

  • In the string ‘kk’, the frequency of ‘k’ is 2, equal to 2^1.

At last, we sorted all strings in decreasing order and showed non-empty strings.

Input – arr[] = {"yz ", "nm", "", ""}

Output – zy, mn

Explanation – It removes empty strings in the output.

Approach 1

In this approach, first, we will count the frequency of each character in the given string. After that, we will check if the frequency is a power of 2. If yes, we will keep the character in the string. Otherwise, we will remove it from the string.

Users can follow the algorithm below to solve the problem.

Algorithm

  • Define the isPowerOfTwo() function to check whether the number is a power of 2.

    • In the function, if ‘num’ equals zero, return false.

    • Take a number log by keeping the base 2.

    • If num is a power of 2, we get the integer value when we take the log. So, use the ceil() and floor() methods to check whether the log value is an integer and return a boolean value based on that.

  • Define the sorted String() function to sort the strings after modifying them

  • Define the ‘freq’ map to store the frequency of characters of a particular string. Also, define the ‘ans’ vector to store the resultant strings.

  • Traverse the string array using the loop. In the loop, define the ‘temp’ string variable and initialize with an empty string.

  • Now, traverse the string from the ith index using the loop, and store its characters’ frequency in the ‘freq’ map.

  • the power of 2. If yes, append character frequency time into the temp string.

  • Use the clear() method to clear the map, as we need to store the frequency of another string’s characters

  • If the size of the ‘temp’ string is zero, continue iteration.

  • Sort the string using the sort() method and append it to the ‘ans’ vector.

  • Iterate through the ‘ans’ vector to get all resultant strings.

Example

#include <iostream>
#include <vector>
#include <unordered_map>
#include <cmath>
#include <algorithm>
using namespace std;

// Check whether Num is power of 2
bool isPowerOfTwo(int num){
   // Base Case
   if (num == 0)
      return false;
   // If num is a power of 2, then log2(num) will be an integer value.
   return (ceil(log2(num)) == floor(log2(num)));
}
// function to modify the array of strings according to the given condition.
void sortedStrings(string str[], int N){
   // map to store the frequency of each alphabet of the string.
   unordered_map<char, int> freq;
   // storing answer in a vector of strings.
   vector<string> ans;
   // iterate over the array of strings
   for (int i = 0; i < N; i++){
      // Temporary string
      string temp = "";
      // Stores frequency of each
      // alphabet of the string
      for (int j = 0; j < str[i].size(); j++){
         // Update frequency of str[i][j]
         freq[str[i][j]]++;
      }
      // Iterate over the map
      for (auto i : freq){
         // If the frequency of any alphabet is a power of 2, then append it to temp.
          if (isPowerOfTwo(i.second)){
             // append i.first character i.second times to temp.
             for (int j = 0; j < i.second; j++){
                 temp += i.first;
             }
          }
      }
      // Clear the map
      freq.clear();
      // empty string
      if (temp.size() == 0)
         continue;
      // sort the string in non-increasing order
      sort(temp.begin(), temp.end(), greater<char>());
      // push temp string to ans
      ans.push_back(temp);
   }
   // Print the array of strings
   cout << "The array of strings after modification is: ";
   for (int i = 0; i < ans.size(); i++){
      cout << ans[i] << " ";
   }
}
int main(){
   string arr[] = {"abde", "cpcc", "ddddd", "kk"};
   int N = sizeof(arr) / sizeof(arr[0]);
   sortedStrings(arr, N);
   return 0;
}

Output

The array of strings after modification is: edba p kk 

Time complexity – O(N*M), where N is the length of the array, and M is the maximum length of the string.

Space complexity – O(N), to store character frequency in the map.

We learned to remove all characters whose frequency is not equal to the power of 2 from the string and sort the string in decreasing order. Programmers can try to sort the characters of the string in their frequency order.

Updated on: 10-Aug-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements