-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteration1.kt
More file actions
23 lines (21 loc) · 814 Bytes
/
Iteration1.kt
File metadata and controls
23 lines (21 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import kotlin.collections.HashMap
class Solution {
fun wordPattern(pattern: String, s: String): Boolean {
val array = s.split(" ")
if (pattern.length != array.size) return false
val hashmap = HashMap<Char, String>()
for (i in array.indices) {
if (hashmap.contains(pattern[i])) {
if (hashmap[pattern[i]] != array[i]) return false
} else if (!hashmap.values.contains(array[i]))
hashmap[pattern[i]] = array[i]
else return false
}
return true
}
}
/**
* If the number of the words and chars in pattern are not equal, return false.
* Else create a map to have something to save checked chars and words in it.
* It is a map because you need to create connection between words and chars.
* */