g2301_2400.s2347_best_poker_hand.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 g2301_2400.s2347_best_poker_hand
// #Easy #Array #Hash_Table #Counting #2023_07_01_Time_146_ms_(33.33%)_Space_34_MB_(66.67%)
class Solution {
fun bestHand(ranks: IntArray, suits: CharArray): String {
val map = HashMap()
for (suit in suits) {
if (map.containsKey(suit)) {
map[suit] = map[suit]!! + 1
if (map[suit] == 5) {
return "Flush"
}
} else {
map[suit] = 1
}
}
var s = ""
val map2 = HashMap()
for (rank in ranks) {
if (map2.containsKey(rank)) {
map2[rank] = map2[rank]!! + 1
if (map2[rank] == 2) {
s = "Pair"
} else if (map2[rank] == 3) {
s = "Three of a Kind"
return s
}
} else {
map2[rank] = 1
}
}
return if (s.isEmpty()) "High Card" else s
}
}