-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.go
41 lines (36 loc) · 710 Bytes
/
solution.go
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
package main
import (
"fmt"
)
func findAndReplacePattern(words []string, pattern string) []string {
var result []string
for _, word := range words {
if match(word, pattern) {
result = append(result, word)
}
}
return result
}
func match(word string, pattern string) bool {
m1 := make(map[byte]byte)
m2 := make(map[byte]byte)
for i := range word {
w := word[i]
p := pattern[i]
if _, ok := m1[w]; !ok {
m1[w] = p
}
if _, ok := m2[p]; !ok {
m2[p] = w
}
if m1[w] != p || m2[p] != w {
return false
}
}
return true
}
func main() {
words := []string{"abc", "deq", "mee", "aqq", "dkd", "ccc"}
pattern := "abb"
fmt.Printf("%#v", findAndReplacePattern(words, pattern))
}