原题
https://leetcode.cn/problems/word-pattern/description/
解法
func wordPattern(pattern string, s string) bool {
words := strings.Split(s, " ")
matchMap := map[byte]string{}
matchMapReverse := map[string]byte{}
if len(words) != len(pattern){
return false
}
for i := 0; i < len(pattern); i++{
if p, ok := matchMapReverse[words[i]]; ok{
if p != pattern[i]{
return false
}
}
if w, ok := matchMap[pattern[i]]; ok{
if w != words[i]{
return false
}
}
matchMap[pattern[i]] = words[i]
matchMapReverse[words[i]] = pattern[i]
}
return true
}
原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode290-%e5%8d%95%e8%af%8d%e8%a7%84%e5%be%8b/