mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
规范格式
This commit is contained in:
31
leetcode/0290.Word-Pattern/290. Word Pattern.go
Normal file
31
leetcode/0290.Word-Pattern/290. Word Pattern.go
Normal file
@ -0,0 +1,31 @@
|
||||
package leetcode
|
||||
|
||||
import "strings"
|
||||
|
||||
func wordPattern(pattern string, str string) bool {
|
||||
strList := strings.Split(str, " ")
|
||||
patternByte := []byte(pattern)
|
||||
if pattern == "" || len(patternByte) != len(strList) {
|
||||
return false
|
||||
}
|
||||
|
||||
pMap := map[byte]string{}
|
||||
sMap := map[string]byte{}
|
||||
for index, b := range patternByte {
|
||||
if _, ok := pMap[b]; !ok {
|
||||
if _, ok = sMap[strList[index]]; !ok {
|
||||
pMap[b] = strList[index]
|
||||
sMap[strList[index]] = b
|
||||
} else {
|
||||
if sMap[strList[index]] != b {
|
||||
return false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if pMap[b] != strList[index] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
Reference in New Issue
Block a user