โจทย์
Programs write log files with one event per line. Each line starts with a level word followed by a space and a message, for example ERROR disk full.
Given the lines of a log file, count the lines whose first word is exactly INFO, WARNING or ERROR. Lines that start with anything else (including lowercase levels) are ignored.
Implement count_log_levels(lines).
Input
lines: The lines of the file, without line breaks.
Output
Return [info_count, warning_count, error_count].
เงื่อนไข
0 ≤ number of lines ≤ 10,000
Each line has at most 200 characters
ตัวอย่าง
อินพุต:
lines = ["INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"]
ผลลัพธ์ที่คาดหวัง:
expected = [2, 1, 1]
อินพุต:
std::vector<std::string> lines = {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};
ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {2, 1, 1};
อินพุต:
string[] lines = new string[] {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};
ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {2, 1, 1};
คำอธิบาย
Two lines start with INFO, one with WARNING and one with ERROR: [2, 1, 1].
อินพุต:
lines = ["info lowercase", "DEBUG details", "ERROR"]
ผลลัพธ์ที่คาดหวัง:
expected = [0, 0, 1]
อินพุต:
std::vector<std::string> lines = {"info lowercase", "DEBUG details", "ERROR"};
ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {0, 0, 1};
อินพุต:
string[] lines = new string[] {"info lowercase", "DEBUG details", "ERROR"};
ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {0, 0, 1};
คำอธิบาย
"info" is not "INFO" and DEBUG is not counted. The last line is just the word ERROR, which counts: [0, 0, 1].
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
Only the first word of each line matters.
คำใบ้ 2
Split once at the first space and compare that word exactly.
คำใบ้ 3
Case matters: "info" is not "INFO", and a line can be just the level word.
ความซับซ้อนที่คาดหวัง
O(total length) time, O(1) extra space
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน