-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2452-WordsWithinTwoEditsOfDictionary.go
More file actions
81 lines (71 loc) · 3.29 KB
/
2452-WordsWithinTwoEditsOfDictionary.go
File metadata and controls
81 lines (71 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
// 2452. Words Within Two Edits of Dictionary
// 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 <= 100
// n == queries[i].length == dictionary[j].length
// 1 <= n <= 100
// All queries[i] and dictionary[j] are composed of lowercase English letters.
import "fmt"
func twoEditWords(queries []string, dictionary []string) []string {
helper := func(word string) bool {
for _, d := range dictionary {
if len(word) != len(d) { continue }
i, count := 0, 0
for i < len(word) {
if word[i] != d[i] { count++ }
if count > 2 { break }
i++
}
if count <= 2 { return true }
}
return false
}
res := []string{}
for _, q := range queries {
if helper(q) {
res = append(res, q)
}
}
return res
}
func main() {
// 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"].
fmt.Println(twoEditWords([]string{"word","note","ants","wood"}, []string{"wood","joke","moat"})) // ["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.
fmt.Println(twoEditWords([]string{"yes"}, []string{"not"})) // []
fmt.Println(twoEditWords([]string{"bluefrog"}, []string{"leetcode"})) // []
fmt.Println(twoEditWords([]string{"bluefrog"}, []string{"frewu"})) // []
fmt.Println(twoEditWords([]string{"frewu"}, []string{"leetcode"})) // []
}