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 fromqueries _,_that match with some word fromdictionaryafter a maximum oftwo edits. Return the words in the
same order they appear in queries.
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"].
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.
Since all words are the same length and edits are letter substitutions, we can simply count the number of differing positions (Hamming distance) between each query and each dictionary word. If any dictionary word is within 2 edits, we include the query in the answer.
import java.util.*;
classSolution {
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) {
ans.add(q);
break;
}
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funtwoEditWords(queries: Array<String>, dictionary: Array<String>): List<String> {
val ans = mutableListOf<String>()
for (q in queries) {
for (d in dictionary) {
var diff = 0for (i in q.indices) {
if (q[i] != d[i]) diff++ }
if (diff <=2) {
ans.add(q)
break }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
from typing import List
classSolution:
deftwoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
ans: List[str] = []
for q in queries:
for d in dictionary:
diff = sum(a != b for a, b in zip(q, d))
if diff <=2:
ans.append(q)
breakreturn ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfntwo_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
letmut ans = Vec::new();
for q in&queries {
for d in&dictionary {
let diff = q.chars().zip(d.chars()).filter(|(a, b)| a != b).count();
if diff <=2 {
ans.push(q.clone());
break;
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
twoEditWords(queries: string[], dictionary: string[]):string[] {
constans: string[] = [];
for (constqofqueries) {
for (constdofdictionary) {
letdiff=0;
for (leti=0; i<q.length; i++) {
if (q[i] !==d[i]) diff++;
}
if (diff<=2) {
ans.push(q);
break;
}
}
}
returnans;
}
}