Created
December 28, 2019 02:46
-
-
Save luoheng23/19a2ce1c0fb2783490e11c0cbaacbceb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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