solution.go 809 B

1234567891011121314151617181920212223242526272829303132333435
  1. func findAndReplacePattern(words []string, pattern string) []string {
  2. result := []string{}
  3. for i := 0; i < len(words); i++ {
  4. if (checkPattern(words[i], pattern)) {
  5. result = append(result, words[i])
  6. }
  7. }
  8. return result;
  9. }
  10. func checkPattern(word string, pattern string) bool {
  11. m1 := make(map[byte]byte)
  12. m2 := make(map[byte]byte)
  13. for i := 0; i < len(word); i++ {
  14. toCheck, hasKey := m1[word[i]]
  15. _, hasValue := m2[pattern[i]]
  16. if (hasKey) {
  17. if (toCheck != pattern[i]) {
  18. return false
  19. }
  20. } else if (hasValue) {
  21. return false;
  22. } else {
  23. m1[word[i]] = pattern[i]
  24. m2[pattern[i]] = word[i]
  25. }
  26. }
  27. return true
  28. }