Score a Guess Against a Hidden Word with Exact and Shifted Hits
StringsAuto-graded · 23 test casesLast updated
Compute exact-position and misplaced letter counts between a guess and a secret using leftover letter counts, with a sentinel for guesses missing from the word list.
You are building the scoring routine for a word-guessing game.
You are given:
word_list: a list of distinct lowercase words, all of the same length.secret: the hidden word.guess: the player's attempted word.
Return a two-element list [exact, shifted] describing how well the guess matches the secret.
Scoring rules
- Validity check. If
guessdoes not appear inword_list, return[-1, -1]. - Exact hits.
exactis the number of indicesiwheresecret[i] == guess[i]. - Shifted hits. Ignore every index that counted as an exact hit. Among the remaining indices, gather the letters of
secretand the letters ofguessseparately. For each letterc, letabe how many timescoccurs in the remaining part ofsecretandbhow many times it occurs in the remaining part ofguess. Thenshiftedis the sum ofmin(a, b)over all lettersc.
Every letter occurrence can be used for at most one pairing, so nothing is counted twice, and a letter already consumed by an exact hit cannot be reused as a shifted hit.
Output
A list of two integers [exact, shifted], or [-1, -1] when the guess is not in word_list.
Constraints
1 <= len(word_list) <= 10^5- Every word in
word_list, as well assecretandguess, has the same lengthL, where1 <= L <= 20. - All words contain only lowercase English letters
atoz. secretis one of the words inword_list.
Examples
Example 1
Input: score_guess(['apple', 'angle', 'ample', 'alley'], 'apple', 'alley')
Output: [1, 2]
Example 2
Input: score_guess(['abbey', 'babbb', 'bbbaa'], 'abbey', 'babbb')
Output: [1, 2]
Example 3
Input: score_guess(['abc', 'bca', 'cab'], 'abc', 'cab')
Output: [0, 3]
Hints
Hint 1
Handle the invalid-guess case first; a set gives fast membership checks.
Hint 2
Count exact positional matches first, and only record the letters from the non-matching positions.
Hint 3
Keep one letter-count table for the secret's leftover letters and one for the guess's leftover letters, then add up the per-letter minimums.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Build a set from
word_listand check whetherguessis in it. If not, return[-1, -1]. - Walk through both words together. When the characters at an index are equal, increase
exact. Otherwise, increment a count for the secret's character in one 26-slot array and a count for the guess's character in another. - After the walk, the two arrays hold only the letters from non-exact positions. For each of the 26 letters, add the smaller of the two counts to
shifted.
Why it works
- Exact hits are decided purely by position, so each index is counted at most once.
- Removing exact positions before counting the rest ensures a letter occurrence that already matched in place cannot also be used as a shifted hit.
- Among the leftover positions, any occurrence of a letter in the guess can be paired with any occurrence of the same letter in the secret, with each occurrence used once. The largest number of pairs for letter
cis thereforemin(a, b). Different letters never compete for the same occurrence, so summing over letters gives the maximum total.
For example, with secret = "abc" and guess = "cab", no index matches, so all letters are leftovers and each letter has a minimum of 1, giving [0, 3].
Complexity
Let N be the number of words and L the word length.
- Time:
O(N * L)to build the set (hashing each word), plusO(L + 26)for scoring. - Space:
O(N * L)for the set; the counting arrays useO(1).
If many guesses were made against the same list, you would build the set once and reuse it, making each guess cost O(L).
Reference solution
from typing import List
def score_guess(word_list: List[str], secret: str, guess: str) -> List[int]:
if guess not in set(word_list):
return [-1, -1]
exact = 0
secret_left = [0] * 26
guess_left = [0] * 26
for s_ch, g_ch in zip(secret, guess):
if s_ch == g_ch:
exact += 1
else:
secret_left[ord(s_ch) - ord('a')] += 1
guess_left[ord(g_ch) - ord('a')] += 1
shifted = sum(min(secret_left[i], guess_left[i]) for i in range(26))
return [exact, shifted]