Skip to content

Commit 21d31ca

Browse files
committed
Create 0290-word-pattern.kt
1 parent 41aa60f commit 21d31ca

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

kotlin/0290-word-pattern.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
fun wordPattern(pattern: String, s: String): Boolean {
3+
val wordArray = s.split(" ")
4+
if(wordArray.size != pattern.length) return false
5+
val hm = HashMap<Char, String>()
6+
pattern.forEachIndexed { i, c ->
7+
when(hm.contains(c)){
8+
true -> if(hm[c] != wordArray[i]) return false
9+
false -> {
10+
if(hm.containsValue(wordArray[i])) return false
11+
hm[c] = wordArray[i]
12+
}
13+
}
14+
}
15+
return true
16+
}
17+
}

0 commit comments

Comments
 (0)