A
A
In Meta infrastructure systems, engineers often need to summarize large volumes of Scuba-style server logs. Given a list of log lines, return the top 5 most frequent error messages.
A log line is considered an error message only if it starts with the prefix "ERROR: ". The message is the substring after that prefix. Count identical messages together and return up to 5 messages ordered by descending frequency. If two messages have the same frequency, return them in lexicographically ascending order.
Implement a function that takes a list of strings logs and returns a list of strings.
logs, a list of log linesIgnore all non-error log lines.
Example 1
Input: logs = ["INFO: boot", "ERROR: disk full", "ERROR: timeout", "ERROR: disk full"]
Output: ["disk full", "timeout"]
Explanation: Only lines starting with "ERROR: " are counted. "disk full" appears twice and "timeout" appears once.
Example 2
Input: logs = ["ERROR: b", "ERROR: a", "ERROR: b", "ERROR: a", "ERROR: c"]
Output: ["a", "b", "c"]
Explanation: "a" and "b" both appear twice, so lexicographic order breaks the tie.
1 <= len(logs) <= 10^50 <= len(logs[i]) <= 200logs = ["INFO: boot", "ERROR: disk full", "ERROR: timeout", "ERROR: disk full"]Output["disk full", "timeout"]WhyOnly two distinct error messages exist. `"disk full"` appears twice, so it comes before `"timeout"`.logs = ["ERROR: b", "ERROR: a", "ERROR: b", "ERROR: a", "ERROR: c"]Output["a", "b", "c"]Why`"a"` and `"b"` both have frequency 2, so lexicographic order puts `"a"` first.logs = ["WARN: cpu high", "INFO: ready"]Output[]WhyThere are no lines starting with `"ERROR: "`, so the result is empty.1 <= len(logs) <= 10^50 <= len(logs[i]) <= 200Each log line is an ASCII stringA valid error line starts exactly with "ERROR: "Return at most 5 messagesIf fewer than 5 distinct error messages exist, return all of themdef top_five_errors(logs):