Skip to content

Commit

Permalink
Create 0290-word-pattern.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Jan 27, 2023
1 parent 41aa60f commit 21d31ca
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions kotlin/0290-word-pattern.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
fun wordPattern(pattern: String, s: String): Boolean {
val wordArray = s.split(" ")
if(wordArray.size != pattern.length) return false
val hm = HashMap<Char, String>()
pattern.forEachIndexed { i, c ->
when(hm.contains(c)){
true -> if(hm[c] != wordArray[i]) return false
false -> {
if(hm.containsValue(wordArray[i])) return false
hm[c] = wordArray[i]
}
}
}
return true
}
}

0 comments on commit 21d31ca

Please sign in to comment.