Trace secret = "1123" and guess = "0111".
Init counts[0..9] = 0, bulls = 0, cows = 0.
Index : 1 vs 0. Not a bull. counts[1] is , no cow from secret side. counts[0] is , no cow from guess side. counts[1]++ → , counts[0]-- → .
Index : 1 vs 1. Bull! bulls = 1.
Index : 2 vs 1. Not a bull. counts[2] is . counts[1] is (positive), so cow! cows = 1. Update counts.
Index : 3 vs 1. Not a bull. No match in counts. Update.
Result: "1A1B".
You scan each position once: time. The counts array has fixed size : space.