Trains the technique from
LeetCode 2452Words Within Two Edits of DictionaryThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A stockroom scanner reads part codes. Every code in use, and every code the scanner reads, is exactly n lowercase letters long.
queries holds the codes the scanner read, in the order it read them, and dictionary holds the codes the stockroom actually stocks. A read code is recoverable when some stocked code can be reached from it by overwriting at most two of its letters. Overwriting a letter replaces the letter in that slot with any other letter; slots are never inserted or removed, since the length is fixed.
Return the slots of queries holding a recoverable code, in increasing order. A slot is listed once however many stocked codes it is close to.
Example 1
The code at slot 0 differs from "wxab" in its last two letters. The code at slot 1 also differs from "wxab" in its last two letters. The code at slot 2 differs from "mnrs" in its last two letters.
Example 2
Slot 0 and the stocked code differ in the first and third letters, which is two slots, so the read code is recoverable.
Example 3
Both read codes differ from "zzzz" in three or more slots, so neither is recoverable and the answer is empty.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def near_match_codes(queries: list[str], dictionary: list[str]) -> list[int]:public List<Integer> nearMatchCodes(String[] queries, String[] dictionary)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.