Skip to content

Commit 73f1fb3

Browse files
authored
290. Word Pattern
1 parent 6216fce commit 73f1fb3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

290.WordPattern.kt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
https://leetcode.com/problems/word-pattern/description
3+
*/
4+
fun wordPattern(pattern: String, s: String): Boolean {
5+
val map: MutableMap<String, Char> = HashMap()
6+
val patternArray = pattern.toCharArray()
7+
val sArray = s.split(" ")
8+
if (patternArray.size != sArray.size) {
9+
return false
10+
}
11+
12+
for (i in sArray.indices) {
13+
val key = sArray[i]
14+
val value = patternArray[i]
15+
16+
if (!map.containsKey(key) && !map.containsValue(value)) {
17+
map[key] = value
18+
} else if (!map.containsKey(key) && map.containsValue(value)) {
19+
return false
20+
} else {
21+
if (map[key] != value) {
22+
return false
23+
}
24+
}
25+
}
26+
27+
return true
28+
}

0 commit comments

Comments
 (0)