Problème
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.
Contraintes
0 ≤ length ≤ 10,000
Exemples
Entrée :
text = "hello"
Sortie attendue :
expected = ["e:1", "h:1", "l:2", "o:1"]
Entrée :
std::string text = "hello";
Sortie attendue :
std::vector<std::string> expected = {"e:1", "h:1", "l:2", "o:1"};
Entrée :
string text = "hello";
Sortie attendue :
string[] expected = new string[] {"e:1", "h:1", "l:2", "o:1"};
Explication
e:1, h:1, l:2, o:1 sorted by character: ["e:1", "h:1", "l:2", "o:1"].
Entrée :
text = "a a A"
Sortie attendue :
expected = ["A:1", "a:2"]
Entrée :
std::string text = "a a A";
Sortie attendue :
std::vector<std::string> expected = {"A:1", "a:2"};
Entrée :
string text = "a a A";
Sortie attendue :
string[] expected = new string[] {"A:1", "a:2"};
Explication
Spaces are skipped; "A" sorts before "a": ["A:1", "a:2"].
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Skip spaces, but count every other ASCII character exactly as it appears.
Indice 2
Accumulate counts in a frequency table, then output distinct characters once.
Indice 3
Sort by character code so uppercase and lowercase order naturally follows ASCII.
Complexité attendue
O(n + m log m) time, O(m) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.