โจทย์
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน