Условие
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.
Ограничения
0 ≤ length ≤ 10,000
Примеры
Ввод:
text = "hello"
Ожидаемый вывод:
expected = ["e:1", "h:1", "l:2", "o:1"]
Ввод:
std::string text = "hello";
Ожидаемый вывод:
std::vector<std::string> expected = {"e:1", "h:1", "l:2", "o:1"};
Ввод:
string text = "hello";
Ожидаемый вывод:
string[] expected = new string[] {"e:1", "h:1", "l:2", "o:1"};
Пояснение
e:1, h:1, l:2, o:1 sorted by character: ["e:1", "h:1", "l:2", "o:1"].
Ввод:
text = "a a A"
Ожидаемый вывод:
expected = ["A:1", "a:2"]
Ввод:
std::string text = "a a A";
Ожидаемый вывод:
std::vector<std::string> expected = {"A:1", "a:2"};
Ввод:
string text = "a a A";
Ожидаемый вывод:
string[] expected = new string[] {"A:1", "a:2"};
Пояснение
Spaces are skipped; "A" sorts before "a": ["A:1", "a:2"].
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
Skip spaces, but count every other ASCII character exactly as it appears.
Подсказка 2
Accumulate counts in a frequency table, then output distinct characters once.
Подсказка 3
Sort by character code so uppercase and lowercase order naturally follows ASCII.
Ожидаемая сложность
O(n + m log m) time, O(m) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.