Given two strings secret and guess of equal length, return a feedback list that labels each position of guess by how its character compares to secret.
Each label is one of:
-
"correct"
when the character in
guess
matches the character in
secret
at the same position.
-
"misplaced"
when the character appears somewhere in
secret
but not at this position.
-
"wrong"
when the character does not appear in
secret
at all, or all of its occurrences have already been accounted for.
Duplicate letters must be handled carefully: no letter in secret may be credited (as "correct" or "misplaced") more times than it actually appears in secret. Exact matches ("correct") take priority over partial matches ("misplaced").
Function Signature
def evaluate_guess(secret: str, guess: str) -> list[str]:
Rules
-
Return a list of length
len(secret)
whose entries are exactly the strings
"correct"
,
"misplaced"
or
"wrong"
.
-
Every position where
guess[i] == secret[i]
is
"correct"
, wherever it sits in the string.
-
Once the exact matches are counted, each letter has some number of occurrences in
secret
left uncredited. The other positions of
guess
are labeled from left to right: a position is
"misplaced"
if its letter still has an uncredited occurrence (which that position then uses up), and
"wrong"
otherwise. When a letter appears in
guess
more often than it can be credited, the leftmost of those positions get
"misplaced"
.
Constraints
-
1 <= len(secret) <= 10^4
-
len(guess) == len(secret)
-
secret
and
guess
consist of lowercase English letters only.
Examples
Input: secret = "aabbc", guess = "aacde"
Output: ["correct", "correct", "misplaced", "wrong", "wrong"]
Positions 0 and 1 are exact matches for a. The c at position 2 is not at that spot in secret, but secret has a c at position 4, so it is misplaced. d and e do not appear in secret.
Input: secret = "hello", guess = "world"
Output: ["wrong", "misplaced", "wrong", "correct", "wrong"]
The l at position 3 matches secret exactly. The o at position 1 is credited with the only o in secret (position 4), so it is misplaced. w, r and d do not appear in secret.
Input: secret = "abbey", guess = "bobby"
Output: ["misplaced", "wrong", "correct", "wrong", "correct"]
Positions 2 (b) and 4 (y) are exact matches. secret has two bs and the exact match uses one, so only one more b can be credited. It goes to the leftmost remaining b in guess at position 0, and the b at position 3 is wrong. o does not appear in secret.