Problem

You are given an integer array ranks and a character array suits. You have 5 cards where the ith card has a rank of ranks[i] and a suit of suits[i].

The following are the types of poker hands you can make from best to worst:

  1. "Flush": Five cards of the same suit.
  2. "Three of a Kind": Three cards of the same rank.
  3. "Pair": Two cards of the same rank.
  4. "High Card": Any single card.

Return a string representing thebest type of poker hand you can make with the given cards.

Note that the return values are case-sensitive.

Examples

Example 1

1
2
3
Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
Output: "Flush"
Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".

Example 2

1
2
3
4
5
Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
Output: "Three of a Kind"
Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
Also note that other cards could be used to make the "Three of a Kind" hand.

Example 3

1
2
3
4
Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
Output: "Pair"
Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
Note that we cannot make a "Flush" or a "Three of a Kind".

Constraints

  • ranks.length == suits.length == 5
  • 1 <= ranks[i] <= 13
  • 'a' <= suits[i] <= 'd'
  • No two cards have the same rank and suit.

Solution

Method 1 – Counting and Hash Table

Intuition

We need to check for the best poker hand in the order: Flush > Three of a Kind > Pair > High Card. If all suits are the same, it’s a Flush. Otherwise, count the frequency of each rank to check for Three of a Kind or Pair.

Approach

  1. If all suits are the same, return “Flush”.
  2. Count the frequency of each rank using a hash table or array.
  3. If any rank appears at least 3 times, return “Three of a Kind”.
  4. If any rank appears at least 2 times, return “Pair”.
  5. Otherwise, return “High Card”.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    string bestHand(vector<int>& ranks, vector<char>& suits) {
        if (all_of(suits.begin(), suits.end(), [&](char s){ return s == suits[0]; }))
            return "Flush";
        unordered_map<int, int> cnt;
        for (int r : ranks) cnt[r]++;
        int mx = 0;
        for (auto& [k, v] : cnt) mx = max(mx, v);
        if (mx >= 3) return "Three of a Kind";
        if (mx == 2) return "Pair";
        return "High Card";
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
func bestHand(ranks []int, suits []byte) string {
    flush := true
    for _, s := range suits {
        if s != suits[0] {
            flush = false
            break
        }
    }
    if flush {
        return "Flush"
    }
    cnt := map[int]int{}
    for _, r := range ranks {
        cnt[r]++
    }
    mx := 0
    for _, v := range cnt {
        if v > mx {
            mx = v
        }
    }
    if mx >= 3 {
        return "Three of a Kind"
    }
    if mx == 2 {
        return "Pair"
    }
    return "High Card"
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public String bestHand(int[] ranks, char[] suits) {
        boolean flush = true;
        for (char s : suits) if (s != suits[0]) flush = false;
        if (flush) return "Flush";
        Map<Integer, Integer> cnt = new HashMap<>();
        for (int r : ranks) cnt.put(r, cnt.getOrDefault(r, 0) + 1);
        int mx = 0;
        for (int v : cnt.values()) mx = Math.max(mx, v);
        if (mx >= 3) return "Three of a Kind";
        if (mx == 2) return "Pair";
        return "High Card";
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun bestHand(ranks: IntArray, suits: CharArray): String {
        if (suits.all { it == suits[0] }) return "Flush"
        val cnt = ranks.groupingBy { it }.eachCount()
        val mx = cnt.values.maxOrNull() ?: 0
        return when {
            mx >= 3 -> "Three of a Kind"
            mx == 2 -> "Pair"
            else -> "High Card"
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def bestHand(self, ranks: list[int], suits: list[str]) -> str:
        if all(s == suits[0] for s in suits):
            return "Flush"
        from collections import Counter
        cnt = Counter(ranks)
        mx = max(cnt.values())
        if mx >= 3:
            return "Three of a Kind"
        if mx == 2:
            return "Pair"
        return "High Card"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
impl Solution {
    pub fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String {
        if suits.iter().all(|&s| s == suits[0]) {
            return "Flush".to_string();
        }
        let mut cnt = std::collections::HashMap::new();
        for &r in &ranks {
            *cnt.entry(r).or_insert(0) += 1;
        }
        let mx = *cnt.values().max().unwrap();
        if mx >= 3 {
            "Three of a Kind".to_string()
        } else if mx == 2 {
            "Pair".to_string()
        } else {
            "High Card".to_string()
        }
    }
}

Complexity

  • ⏰ Time complexity: O(1) — Since the number of cards is always 5.
  • 🧺 Space complexity: O(1) — Only a small hash table is used.