2452. Words Within Two Edits of Dictionary
🧩 Problem:
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 <= 100n == queries[i].length == dictionary[j].length1 <= n <= 100- All
queries[i]anddictionary[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