-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_find.go
33 lines (31 loc) · 1.06 KB
/
pattern_find.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
package problem0890
/*
Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.
*/
func findAndReplacePattern(words []string, pattern string) []string {
var result = []string{}
var legend = map[byte]int{}
// Building our dictionary for where each letter points to
for idx := range pattern {
if _, ok := legend[pattern[idx]]; !ok {
legend[pattern[idx]] = idx
}
}
EachWord:
for _, word := range words {
var freq = map[byte]bool{}
for idx := range word {
freq[word[idx]] = true
legendidx := legend[pattern[idx]]
if word[idx] != word[legendidx] {
continue EachWord
}
}
if len(legend) == len(freq) {
result = append(result, word)
}
}
return result
}