luoheng
12/28/2019 - 2:46 AM

wordPattern

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
}