All challenges
Easy 0 solved 0% acceptance

Character Frequency

Count how often each character appears.

Problem

Count every character in text except spaces. Report each distinct character once as "character:count", sorted by character code (so uppercase letters come before lowercase).

Implement character_frequency(text).

Input

text: The text to count (ASCII characters).

Output

Return the list of "character:count" entries.

Constraints

0 ≤ length ≤ 10,000

Examples

Example 1
Input:
text = "hello"

Expected output:
expected = ["e:1", "h:1", "l:2", "o:1"]
Input:
std::string text = "hello";

Expected output:
std::vector<std::string> expected = {"e:1", "h:1", "l:2", "o:1"};
Input:
string text = "hello";

Expected output:
string[] expected = new string[] {"e:1", "h:1", "l:2", "o:1"};

Explanation

e:1, h:1, l:2, o:1 sorted by character: ["e:1", "h:1", "l:2", "o:1"].

Example 2
Input:
text = "a a A"

Expected output:
expected = ["A:1", "a:2"]
Input:
std::string text = "a a A";

Expected output:
std::vector<std::string> expected = {"A:1", "a:2"};
Input:
string text = "a a A";

Expected output:
string[] expected = new string[] {"A:1", "a:2"};

Explanation

Spaces are skipped; "A" sorts before "a": ["A:1", "a:2"].

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.