Dashboard Temp Share Shortlinks Frames API

HTMLify

LeetCode - Words Within Two Edits of Dictionary - Go
Views: 8 | Author: abh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func wordDiff(word1 string, word2 string) int {
	diff := 0
	l := len(word1)
	for i := range l {
		if word1[i] != word2[i] {
			diff++
		}
	}
	return diff
}

func twoEditWords(queries []string, dictionary []string) []string {
	var answers []string
	for _, q := range queries {
		for _, d := range dictionary {
			if wordDiff(q, d) <= 2 {
				answers = append(answers, q)
				break
			}
		}
	}
	return answers
}