-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution290.go
More file actions
41 lines (38 loc) · 749 Bytes
/
solution290.go
File metadata and controls
41 lines (38 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package solution290
import (
"strings"
)
// ============================================================================
// 290. Word Pattern
// URL: https://leetcode.com/problems/word-pattern/
// ============================================================================
func wordPattern(pattern string, s string) bool {
var (
m = make(map[byte]string)
ch byte
)
words := strings.Fields(s)
if len(pattern) != len(words) {
return false
}
for i, rune := range pattern {
ch = byte(rune)
v, ok := m[ch]
if !ok {
for _, word := range m {
if word == words[i] {
return false
}
}
m[ch] = words[i]
continue
}
if v == words[i] {
continue
}
if v != words[i] {
return false
}
}
return true
}