[LeetCode] Best Poker Hand

2347. Best Poker Hand

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 the best type of poker hand you can make with the given cards.

Note that the return values are case-sensitive.

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) {
unordered_set<char> us(begin(suits), end(suits));
if(us.size() == 1) return"Flush";
unordered_map<int, int> freq;
for(auto r : ranks) freq[r]++;
int ma = 0;
for(auto [_,c] : freq) ma = max(ma,c);
if(ma >= 3) return "Three of a Kind";
if(ma == 2) return "Pair";
return "High Card";
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/24/PS/LeetCode/best-poker-hand/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.