Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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:
"Flush"
: Five cards of the same suit."Three of a Kind"
: Three cards of the same rank."Pair"
: Two cards of the same rank."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.
Example 1:
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:
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:
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'
class Solution {
public:
string bestHand(vector<int>& ranks, vector<char>& suits) {
map<char,int> m ; // Stores count of same suit
map<int,int> m2 ; // Stores count of same rank
for(auto i:suits)
{
m[i]++ ;
}
for(auto i:ranks)
{
m2[i]++ ;
}
string ans = "High Card" ;
for(auto i:m) // Checking same suit
{
if(i.second == 5)
{
ans = "Flush" ;
break ;
}
}
for(auto i:m2) // Checking same rank
{
if(i.second >= 3)
{
ans = "Three of a Kind" ;
break ;
}
else if(i.second == 2)
{
ans = "Pair" ;
break ;
}
}
return ans ;
}
};
class Solution(object):
def bestHand(self, ranks, suits):
"""
:type ranks: List[int]
:type suits: List[str]
:rtype: str
"""
cnt = max(Counter(ranks).values()) # Counts maximum number of ranks of the same kind
ans = "High Card"
if(len(set(suits)) == 1): # 5 of the same
ans = "Flush"
elif cnt >= 3:
ans = "Three of a Kind"
elif cnt == 2:
ans = "Pair"
return ans
class Solution {
public String bestHand(int[] ranks, char[] suits) {
int []hashr = new int[15] ; // Check Constraints, Stores count of ranks
int []hashs = new int[5] ; // Check Constraints, Stores count of suits
for(int i = 0 ; i < 5 ; i++)
{
hashr[ranks[i]] += 1 ;
hashs[suits[i] - 'a'] += 1 ;
}
String ans = "High Card" ;
for(int i = 0 ; i < 5 ; i++) // Checking same suit
{
if(hashs[i] == 5)
{
ans = "Flush" ;
break ;
}
}
for(int i = 0 ; i < 14 ; i++) // Checking same rank
{
if(hashr[i] >= 3)
{
ans = "Three of a Kind" ;
break ;
}
else if(hashr[i] == 2)
{
ans = "Pair" ;
break ;
}
}
return ans ;
}
}
In our experience, we suggest you solve this Best Poker Hand LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Best Poker Hand LeetCode Solution
I hope this Best Poker Hand LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>