All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2301_2400.s2347_best_poker_hand.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy