← Back to list

2452. Words Within Two Edits of Dictionary

🧩 Problem:

Sara | Software Developer & Tech Writer · 2026-04-22 06:07 · 0 claps · 0.8 min read
#java #coding #problem-solving #leetcode #2-4-52
Open on Medium ↗
Wiki topics: 💻 · Programming

2452. Words Within Two Edits of Dictionary

🧩 Problem:

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.

Constraints:

  • 1 <= queries.length, dictionary.length <= 100
  • n == queries[i].length == dictionary[j].length
  • 1 <= n <= 100
  • All queries[i] and dictionary[j] are composed of lowercase English letters.

💡 Key Idea:

For each query word, check if it can be converted into any dictionary word with at most 2 character changes.

Solution:

Time: O(q × d × L)

class Solution {

public List<String> twoEditWords(String[] queries, String[] dictionary) {

List<String> ans = new ArrayList<>();

for(String q : queries){

for(String d : dictionary){

int diff =0;

for(int i=0; i<q.length(); i++){

if(q.charAt(i) != d.charAt(i)){

diff++;

}

if(diff >2){

break;

}

}

if(diff <= 2){

ans.add(q);

break;

}

}

}

return ans;

}

}


메타데이터
post_id
6dc4f144fec9
slug
2452-words-within-two-edits-of-dictionary-6dc4f144fec9
url
https://medium.com/@sarawrites/2452-words-within-two-edits-of-dictionary-6dc4f144fec9
canonical_url
https://medium.com/@sarawrites/2452-words-within-two-edits-of-dictionary-6dc4f144fec9
author_url
https://medium.com/@sarawrites
status
ok
fetched_at
2026-07-11 02:24:24