Skip to content

Commit 701cc7a

Browse files
committed
Create 290. Word Pattern
Signed-off-by: Tahsin Tunan <[email protected]>
1 parent 5de2d4e commit 701cc7a

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

go/0283-move-zeros.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

go/0290-word-pattern.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func wordPattern(pattern string, s string) bool {
2+
words := strings.Split(s, " ")
3+
if len(pattern) != len(words) {
4+
return false
5+
}
6+
7+
seenChr, seenWord := make(map[byte]int, len(pattern)), make(map[string]int, len(pattern))
8+
for i := 0; i < len(pattern); i++ {
9+
chrPos, chrOk := seenChr[pattern[i]]
10+
wordPos, wordOk := seenWord[words[i]]
11+
if chrOk != wordOk || chrPos != wordPos {
12+
return false
13+
}
14+
15+
seenChr[pattern[i]] = i
16+
seenWord[words[i]] = i
17+
}
18+
19+
return true
20+
}

0 commit comments

Comments
 (0)