g2101_2200.s2103_rings_and_rods.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2101_2200.s2103_rings_and_rods
// #Easy #String #Hash_Table #2023_06_25_Time_131_ms_(89.47%)_Space_33.9_MB_(100.00%)
class Solution {
fun countPoints(rings: String): Int {
val redHashMap: MutableMap = HashMap()
val greenHashMap: MutableMap = HashMap()
val blueHashMap: MutableMap = HashMap()
run {
var i = 0
while (i <= rings.length - 2) {
val charOne = rings[i]
val charTwo = rings[i + 1]
if (charOne == 'R') {
redHashMap[Character.getNumericValue(charTwo)] = 123
} else if (charOne == 'G') {
greenHashMap[Character.getNumericValue(charTwo)] = 123
} else {
blueHashMap[Character.getNumericValue(charTwo)] = 123
}
i = i + 2
}
}
var result = 0
for (i in 0..9) {
if (redHashMap.containsKey(i) &&
greenHashMap.containsKey(i) &&
blueHashMap.containsKey(i)
) {
result++
}
}
return result
}
}