Back to challenges
Easy 0 solved 0% acceptance

Character Frequency

Count how often each character appears.

What you'll learn

  • Build a frequency table
  • Update dictionary entries safely
  • Return results in a required order

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"].

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Skip spaces, but count every other ASCII character exactly as it appears.

Hint 2

Accumulate counts in a frequency table, then output distinct characters once.

Hint 3

Sort by character code so uppercase and lowercase order naturally follows ASCII.

Expected complexity

O(n + m log m) time, O(m) extra space

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.