Skip to content

Commit

Permalink
Create 290. Word Pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Tahsin Tunan <[email protected]>
  • Loading branch information
tahsintunan committed Jan 4, 2023
1 parent 5de2d4e commit 701cc7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
13 changes: 0 additions & 13 deletions go/0283-move-zeros.go

This file was deleted.

20 changes: 20 additions & 0 deletions go/0290-word-pattern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func wordPattern(pattern string, s string) bool {
words := strings.Split(s, " ")
if len(pattern) != len(words) {
return false
}

seenChr, seenWord := make(map[byte]int, len(pattern)), make(map[string]int, len(pattern))
for i := 0; i < len(pattern); i++ {
chrPos, chrOk := seenChr[pattern[i]]
wordPos, wordOk := seenWord[words[i]]
if chrOk != wordOk || chrPos != wordPos {
return false
}

seenChr[pattern[i]] = i
seenWord[words[i]] = i
}

return true
}

0 comments on commit 701cc7a

Please sign in to comment.