How Letter Lock handles duplicate letters (the two-pass check)

Anyone who plays five-letter word games has seen it: you guess a word with two identical letters and only one of them turns yellow, or the second one turns grey even though the letter is in the word. This is not a bug. It follows from a precise rule, and getting the rule right in code takes a little care.

The rule

A tile is green when the letter is correct and in the right position. A tile is yellow when the letter is in the hidden word at another position, but only as many times as it still appears after the green matches are taken out. Every other tile is grey. So a guess can never show more yellow and green tiles for a letter than the hidden word actually contains.

Why one pass is not enough

A simple checker walks through the guess from left to right and asks "is this letter anywhere in the word?". That fails when a repeated letter appears before its green match. Take the hidden word ALOFT and the guess LLAMA. The word contains a single L, in second position. A one-pass checker sees the first L, finds an L in the word and marks it yellow. When it reaches the second L it is correct, but the single L has already been used up. The result suggests a second L that does not exist.

The two-pass solution

Letter Lock checks the guess in two passes:

  1. First pass: compare the guess and the hidden word position by position. Matching positions become green. Every letter of the hidden word that was not matched goes into a tally of unmatched letters.
  2. Second pass: walk through the guess from left to right. For each tile that is not green, look in the tally. If the letter is still there, mark the tile yellow and remove one copy from the tally. If not, mark it grey.

With ALOFT and LLAMA, the first pass makes the second L green and uses it up. In the second pass the first L finds nothing left in the tally and turns grey, and only the first A turns yellow, since the hidden word has one A. The correct result is grey, green, yellow, grey, grey.

A second example: the hidden word is ABIDE and you guess SPEED. There are no green tiles. The hidden word has a single E, so the first E in your guess claims it and turns yellow, while the second E turns grey. The D turns yellow. A grey tile on a repeated letter therefore tells you something useful: there are not more copies of it.

The on-screen keyboard

The keyboard remembers the best result seen for each letter, in the order grey, yellow, green. A key can only be upgraded, never downgraded. This means a grey tile on a repeated letter never darkens a key that you already know is yellow or green.

Guesses and scoring

Your guess has to be a valid word from the game's dictionary. If it is not, you see a message and the attempt does not count as a guess. The number of guesses depends on the difficulty: 7 on easy, 6 on medium and 5 on hard.

A word solved on the first guess scores 1,000 points. The score then falls in equal steps down to 300 points for solving it on the last allowed guess. On medium, that works out as 1,000, 860, 720, 580, 440 and 300 points. On hard it is 1,000, 825, 650, 475 and 300. An unsolved word scores nothing. A round has three words, so the maximum is 3,000 points.

A practical tip

An opening guess with five different letters gives you the most information. Once you know a letter appears twice, spend a guess on a word that places it in two different positions instead of repeating the same letters.

Play Letter Lock

← All articles