問題
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 つずつ開いてみましょう。
ヒント 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用に調整されています。上記の説明と制約に従ってください。