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'
No two cards have the same rank and suit.
Solutions
Solution 1
1 2 3 4 5 6 7 8 91011
classSolution:defbestHand(self,ranks:List[int],suits:List[str])->str:# if len(set(suits)) == 1:ifall(a==bfora,binpairwise(suits)):return'Flush'cnt=Counter(ranks)ifany(v>=3forvincnt.values()):return'Three of a Kind'ifany(v==2forvincnt.values()):return'Pair'return'High Card'
1 2 3 4 5 6 7 8 91011121314151617181920
classSolution{publicStringbestHand(int[]ranks,char[]suits){booleanflush=true;for(inti=1;i<5&&flush;++i){flush=suits[i]==suits[i-1];}if(flush){return"Flush";}int[]cnt=newint[14];booleanpair=false;for(intx:ranks){if(++cnt[x]==3){return"Three of a Kind";}pair=pair||cnt[x]==2;}returnpair?"Pair":"High Card";}}
1 2 3 4 5 6 7 8 9101112131415161718192021
classSolution{public:stringbestHand(vector<int>&ranks,vector<char>&suits){boolflush=true;for(inti=1;i<5&&flush;++i){flush=suits[i]==suits[i-1];}if(flush){return"Flush";}intcnt[14]{};boolpair=false;for(int&x:ranks){if(++cnt[x]==3){return"Three of a Kind";}pair|=cnt[x]==2;}returnpair?"Pair":"High Card";}};
1 2 3 4 5 6 7 8 910111213141516171819202122
funcbestHand(ranks[]int,suits[]byte)string{flush:=truefori:=1;i<5&&flush;i++{flush=suits[i]==suits[i-1]}ifflush{return"Flush"}cnt:=[14]int{}pair:=falsefor_,x:=rangeranks{cnt[x]++ifcnt[x]==3{return"Three of a Kind"}pair=pair||cnt[x]==2}ifpair{return"Pair"}return"High Card"}
1 2 3 4 5 6 7 8 91011121314151617
functionbestHand(ranks:number[],suits:string[]):string{if(suits.every(v=>v===suits[0])){return'Flush';}constcount=newArray(14).fill(0);letisPair=false;for(constvofranks){if(++count[v]===3){return'Three of a Kind';}isPair=isPair||count[v]===2;}if(isPair){return'Pair';}return'High Card';}
1 2 3 4 5 6 7 8 9101112131415161718
implSolution{pubfnbest_hand(ranks:Vec<i32>,suits:Vec<char>)->String{ifsuits.iter().all(|v|*v==suits[0]){return"Flush".to_string();}letmutcount=[0;14];letmutis_pair=false;for&vinranks.iter(){leti=vasusize;count[i]+=1;ifcount[i]==3{return"Three of a Kind".to_string();}is_pair=is_pair||count[i]==2;}(ifis_pair{"Pair"}else{"High Card"}).to_string()}}
1 2 3 4 5 6 7 8 9101112131415161718192021222324
char*bestHand(int*ranks,intranksSize,char*suits,intsuitsSize){boolisFlush=true;for(inti=1;i<suitsSize;i++){if(suits[0]!=suits[i]){isFlush=false;break;}}if(isFlush){return"Flush";}intcount[14]={0};boolisPair=false;for(inti=0;i<ranksSize;i++){if(++count[ranks[i]]==3){return"Three of a Kind";}isPair=isPair||count[ranks[i]]==2;}if(isPair){return"Pair";}return"High Card";}