1234567891011121314151617181920212223242526272829303132333435 |
- func findAndReplacePattern(words []string, pattern string) []string {
- result := []string{}
-
- for i := 0; i < len(words); i++ {
- if (checkPattern(words[i], pattern)) {
- result = append(result, words[i])
- }
- }
-
- return result;
- }
- func checkPattern(word string, pattern string) bool {
- m1 := make(map[byte]byte)
- m2 := make(map[byte]byte)
-
- for i := 0; i < len(word); i++ {
- toCheck, hasKey := m1[word[i]]
- _, hasValue := m2[pattern[i]]
-
- if (hasKey) {
- if (toCheck != pattern[i]) {
- return false
- }
- } else if (hasValue) {
- return false;
- } else {
- m1[word[i]] = pattern[i]
- m2[pattern[i]] = word[i]
- }
- }
-
- return true
- }
|