833. Calculate Frequency of Characters in a StringPOTD

You are given a string s. Your task is to calculate the frequency of each character in the string and return the result in a specific order.

The result should consist of each distinct character followed by its frequency, sorted by character in ascending (lexicographical) order.

Example 1:

Input: s = "takeuforward"

Output: "a2 d1 e1 f1 k1 o1 r2 t1 u1 w1"

Explanation: Frequencies sorted by character: a → 2, d → 1, ..., w → 1

Example 2:

Input: s = "articles"

Output: "a1 c1 e1 i1 l1 r1 s1 t1"

Explanation: Sorted order: a, c, e, i, l, r, s, t

Now Your Turn!

Pick the correct output for the given input

Input: s = "aabbcc"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 105
  • The string contains only lowercase English letters ('a' to 'z')
0
class Solution {
public:
string characterFrequency(const string& s) {
// Your code goes here
}
};
 
Test Case

Input:

S