Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created December 28, 2019 02:46
Show Gist options
  • Save luoheng23/19a2ce1c0fb2783490e11c0cbaacbceb to your computer and use it in GitHub Desktop.
Save luoheng23/19a2ce1c0fb2783490e11c0cbaacbceb to your computer and use it in GitHub Desktop.
import "strings"
func wordPattern(pattern string, str string) bool {
var res1 [27]string
res2 := map[string]int{}
str_list := strings.Split(str, " ")
if len(str_list) != len(pattern) {
return false
}
for i := 0; i < len(pattern); i++ {
ind := int(pattern[i] - 'a') + 1
if res2[str_list[i]] != 0 && res2[str_list[i]] != ind ||
res1[ind] != "" && res1[ind] != str_list[i] {
return false
}
res1[ind] = str_list[i]
res2[str_list[i]] = ind
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment