Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two string arrays, queries and dictionary. All words in each array comprise of lowercase English letters and have the same length.
In one edit you can take a word from queries, and change any letter in it to any other letter. Find all words from queries that, after a maximum of two edits, equal some word from dictionary.
Return a list of all words from queries, that match with some word from dictionary after a maximum of two edits. Return the words in the same order they appear in queries.
Example 1:
Input: queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"] Output: ["word","note","wood"] Explanation: - Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood". - Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke". - It would take more than 2 edits for "ants" to equal a dictionary word. - "wood" can remain unchanged (0 edits) and match the corresponding dictionary word. Thus, we return ["word","note","wood"].
Example 2:
Input: queries = ["yes"], dictionary = ["not"] Output: [] Explanation: Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.
Constraints:
1 <= queries.length, dictionary.length <= 100n == queries[i].length == dictionary[j].length1 <= n <= 100queries[i] and dictionary[j] are composed of lowercase English letters.Problem summary: You are given two string arrays, queries and dictionary. All words in each array comprise of lowercase English letters and have the same length. In one edit you can take a word from queries, and change any letter in it to any other letter. Find all words from queries that, after a maximum of two edits, equal some word from dictionary. Return a list of all words from queries, that match with some word from dictionary after a maximum of two edits. Return the words in the same order they appear in queries.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Trie
["word","note","ants","wood"] ["wood","joke","moat"]
["yes"] ["not"]
word-ladder)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2452: Words Within Two Edits of Dictionary
class Solution {
public List<String> twoEditWords(String[] queries, String[] dictionary) {
List<String> ans = new ArrayList<>();
int n = queries[0].length();
for (var s : queries) {
for (var t : dictionary) {
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (s.charAt(i) != t.charAt(i)) {
++cnt;
}
}
if (cnt < 3) {
ans.add(s);
break;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2452: Words Within Two Edits of Dictionary
func twoEditWords(queries []string, dictionary []string) (ans []string) {
for _, s := range queries {
for _, t := range dictionary {
cnt := 0
for i := range s {
if s[i] != t[i] {
cnt++
}
}
if cnt < 3 {
ans = append(ans, s)
break
}
}
}
return
}
# Accepted solution for LeetCode #2452: Words Within Two Edits of Dictionary
class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
ans = []
for s in queries:
for t in dictionary:
if sum(a != b for a, b in zip(s, t)) < 3:
ans.append(s)
break
return ans
// Accepted solution for LeetCode #2452: Words Within Two Edits of Dictionary
impl Solution {
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
queries
.into_iter()
.filter(|s| {
dictionary
.iter()
.any(|t| s.chars().zip(t.chars()).filter(|&(a, b)| a != b).count() < 3)
})
.collect()
}
}
// Accepted solution for LeetCode #2452: Words Within Two Edits of Dictionary
function twoEditWords(queries: string[], dictionary: string[]): string[] {
const n = queries[0].length;
return queries.filter(s => {
for (const t of dictionary) {
let diff = 0;
for (let i = 0; i < n; ++i) {
if (s[i] !== t[i]) {
++diff;
}
}
if (diff < 3) {
return true;
}
}
return false;
});
}
Use this to step through a reusable interview workflow for this problem.
Store all N words in a hash set. Each insert/lookup hashes the entire word of length L, giving O(L) per operation. Prefix queries require checking every stored word against the prefix — O(N × L) per prefix search. Space is O(N × L) for storing all characters.
Each operation (insert, search, prefix) takes O(L) time where L is the word length — one node visited per character. Total space is bounded by the sum of all stored word lengths. Tries win over hash sets when you need prefix matching: O(L) prefix search vs. checking every stored word.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.