Given a string **Str **of length **N**, consisting of lowercase alphabets, the task is to generate a number in decreasing order of the frequency of characters in the given string. If two characters have the same frequency, the character with a smaller ASCII value appears first. Numbers assigned to characters {a, b, …., y, z} are {1, 2, …., 25, 26} respectively. **Note:** For characters having values greater than **9** assigned to it, take its modulo 10.**Examples:**

Input:N = 6, Str = “aaabbd”Output:124Explanation:

Given characters and their respective frequencies are:

- a = 3
- b = 2
- d = 1
Since the number needs to be generated in increasing order of their frequencies, the final generated number is

124.Input:N = 6, Str = “akkzzz”Output:611Explanation:

Given characters and their respective frequencies are:

- a = 1
- k = 2
- z = 3
For

z, value to assigned =26

Hence, the corresponding digit assigned =26 % 10 = 6

Fork, value to assigned =11

Hence, the corresponding digit assigned =11 % 10 = 1

Since the number needs to be generated in increasing order of their frequencies, the final generated number is611.

**Approach:**

Follow the steps below to solve the problem:

- Initialize a Map and store the frequencies of each character.
- Traverse the
**Map**and insert all {**Character, Frequency**} pairs in a vector of pair. - Sort this vector in a way such that the
*pair with higher frequency appears first*and among pairs having the same frequency, those with smaller ASCII value come first. - Traverse this vector and find the digit corresponding to each character.
- Print the final number generated.

Below is the implementation of the above approach:

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;`
`// Custom comparator for sorting` `bool` `comp(pair<` `char` `, ` `int` `>& p1,`
` ` `pair<` `char` `, ` `int` `>& p2)`
`{` ` ` `// If frequency is same`
` ` `if` `(p1.second == p2.second)`
` ` `// Character with lower ASCII`
` ` `// value appears first`
` ` `return` `p1.first < p2.first;`
` ` `// Otherwise character with higher`
` ` `// frequency appears first`
` ` `return` `p1.second > p2.second;`
`}` `// Function to sort map accordingly` `string sort(map<` `char` `, ` `int` `>& m)`
`{` ` ` `// Declaring vector of pairs`
` ` `vector<pair<` `char` `, ` `int` `> > a;`
` ` `// Output string to store the result`
` ` `string out;`
` ` `// Traversing map and pushing`
` ` `// pairs to vector`
` ` `for` `(` `auto` `x : m) {`
` ` `a.push_back({ x.first, x.second });`
` ` `}`
` ` `// Using custom comparator`
` ` `sort(a.begin(), a.end(), comp);`
` ` `// Traversing the Vector`
` ` `for` `(` `auto` `x : a) {`
` ` `// Get the possible digit`
` ` `// from assigned value`
` ` `int` `k = x.first - ` `'a'` `+ 1;`
` ` `// Ensures k does not exceed 9`
` ` `k = k % 10;`
` ` `// Apppend each digit`
` ` `out = out + to_string(k);`
` ` `}`
` ` `// Returning final result`
` ` `return` `out;`
`}` `// Function to generate and return` `// the required number` `string formString(string s)` `{` ` ` `// Stores the frequencies`
` ` `map<` `char` `, ` `int` `> mp;`
` ` `for` `(` `int` `i = 0; i < s.length(); i++)`
` ` `mp[s[i]]++;`
` ` `// Sort map in required order`
` ` `string res = sort(mp);`
` ` `// Return the final result`
` ` `return` `res;`
`}` `// Driver Code` `int` `main()`
`{` ` ` `int` `N = 4;`
` ` `string Str = ` `"akkzzz"` `;`
` ` `cout << formString(Str);`
` ` `return` `0;`
`}` |

*chevron_right*

*filter_none*

`# Python3 Program to implement ` `# the above approach` `# Function to sort map ` `# accordingly` `def` `sort(m):`
` ` `# Declaring vector `
` ` `# of pairs`
` ` `a ` `=` `{}`
` ` `# Output string to `
` ` `# store the result`
` ` `out ` `=` `""`
` ` `# Traversing map and`
` ` `# pushing pairs to vector`
` ` `for` `x ` `in` `m:`
` ` `a[x] ` `=` `[]`
` ` `a[x].append(m[x])`
` ` `# Character with lower ASCII `
` ` `# value appears first`
` ` `a ` `=` `dict` `(` `sorted` `(a.items(), `
` ` `key ` `=` `lambda` `x : x[` `0` `]))`
` ` `# Character with higher `
` ` `# frequency appears first `
` ` `a ` `=` `dict` `(` `sorted` `(a.items(), `
` ` `reverse ` `=` `True` `, `
` ` `key ` `=` `lambda` `x : x[` `1` `]))`
` ` `# Traversing the Vector`
` ` `for` `x ` `in` `a:`
` ` `# Get the possible digit `
` ` `# from assigned value`
` ` `k ` `=` `ord` `(x[` `0` `]) ` `-` `ord` `(` `'a'` `) ` `+` `1`
` ` `# Ensures k does `
` ` `# not exceed 9`
` ` `k ` `=` `k ` `%` `10`
` ` `# Apppend each digit`
` ` `out ` `=` `out ` `+` `str` `(k)`
` ` `# Returning final result`
` ` `return` `out`
`# Function to generate and return ` `# the required number` `def` `formString(s):`
` ` `# Stores the frequencies`
` ` `mp ` `=` `{}`
` ` `for` `i ` `in` `range` `(` `len` `(s)):`
` ` `if` `s[i] ` `in` `mp:`
` ` `mp[s[i]] ` `+` `=` `1`
` ` `else` `:`
` ` `mp[s[i]] ` `=` `1`
` ` `# Sort map in `
` ` `# required order`
` ` `res ` `=` `sort(mp)`
` ` `# Return the `
` ` `# final result`
` ` `return` `res`
`# Driver Code ` `N ` `=` `4`
`Str` `=` `"akkzzz"`
`print` `(formString(` `Str` `))`
`# This code is contributed by avanitrachhadiya2155` |

*chevron_right*

*filter_none*

**Output:**

611

**Time Complexity: **O(NlogN) **Auxiliary Space: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Print characters having odd frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Print characters having even frequencies in order of occurrence
- Print characters having prime frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using Binary Tree
- Print characters and their frequencies in order of occurrence
- Remove characters from given string whose frequencies are a Prime Number
- Generate a string with maximum possible alphabets with odd frequencies
- Binary string with given frequencies of sums of consecutive pairs of characters
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Print characters in decreasing order of frequency
- Sum and Product of Prime Frequencies of Characters in a String
- XOR of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Print 2-D co-ordinate points in ascending order followed by their frequencies
- Queries for frequencies of characters in substrings
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.