-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0890_find_and_replace_pattern.go
More file actions
57 lines (49 loc) · 1.25 KB
/
s0890_find_and_replace_pattern.go
File metadata and controls
57 lines (49 loc) · 1.25 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
/*
https://leetcode.com/problems/find-and-replace-pattern/
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.
*/
package solutions
func findAndReplacePattern(words []string, pattern string) []string {
isValid := func(pattern, s string) bool {
if len(pattern) != len(s) {
return false
}
m := make(map[byte]byte)
for i := 0; i < len(s); i++ {
w, p := s[i], pattern[i]
if v, ok := m[w]; ok {
if v != p {
return false
}
} else {
m[w] = p
}
}
seen := make([]bool, 26)
for _, v := range m {
if seen[v-'a'] {
return false
}
seen[v-'a'] = true
}
return true
}
return filter(func(el string) bool { return isValid(pattern, el) }, words)
}
func filter(pred func(string) bool, ws []string) []string {
var res []string
for _, w := range ws {
if pred(w) {
res = append(res, w)
}
}
return res
}