Count Faults
Problem statement
There are n servers with IDs s1, s2, ..., sn. You are given an array logs in chronological order. Each entry has the form "<server_id> <status>", where status is either success or error.
Track consecutive errors separately for each server. An entry for another server does not interrupt a server's streak. A success resets that server's streak to zero. Whenever a server reaches three consecutive errors, it is considered faulty and is immediately replaced by a new server with the same ID; after replacement, that ID's error streak also resets to zero.
Return the total number of server replacements recorded while processing all logs.
Function
countFaults(n: int, logs: String[]) → intExamples
Example 1
n = 2logs = ["s1 error", "s1 error", "s2 error", "s1 error", "s1 error", "s2 success"]return = 1
Server s1 logs errors on its first, second, and third requests. The intervening request for s2 does not break s1's streak, so s1 is replaced after its third error. Its following error starts a new streak. Server s2 never reaches three consecutive errors because its later success resets its streak. Therefore, exactly one replacement occurs.
Constraints
1 <= n <= 2001 <= logs.length <= 2 * 10^4- Every log contains one of the server IDs
s1throughsnfollowed by eithersuccessorerror.