-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteration1.kt
More file actions
17 lines (16 loc) · 761 Bytes
/
Iteration1.kt
File metadata and controls
17 lines (16 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
fun checkAlmostEquivalent(word1: String, word2: String): Boolean {
val map = HashMap<Char, Int>()
for (index in word1.indices) {
if (word1[index] == word2[index]) continue
map[word1[index]] = map[word1[index]]?.plus(1) ?: 1
map[word2[index]] = map[word2[index]]?.minus(1) ?: -1
}
return map.values.count { x -> x > 3 || x < -3 } == 0
}
}
/**
* I used a map to store the characters and their appearance counts.
* For each appearance in the first word, add one to the count, for each appearance in the second word, minus one from the count.
* If the total sum of their appearance is bigger than 3 or smaller than -3, these two strings are not AlmostEquivalent.
* */