Best Poker Hand
1
2
3
4
5
6
7
8
9
10
11
12
13
| from collections import Counter
class Solution:
def bestHand(self, r: List[int], s: List[str]) -> str:
if max(s) == min(s):
return "Flush"
match max(Counter(r).values()):
case 5 | 4 | 3:
return "Three of a Kind"
case 2:
return "Pair"
case _:
return "High Card"
|
Simple Version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| class Solution:
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
max_rank_count = 0
suit_count = 0
ch = suits[0]
arr = [0] * 14
for i in range(len(ranks)):
arr[ranks[i]] += 1
max_rank_count = max(max_rank_count, arr[ranks[i]])
if suits[i] == ch:
suit_count += 1
if suit_count == 5:
return "Flush"
if max_rank_count >= 3:
return "Three of a Kind"
elif max_rank_count == 2:
return "Pair"
else:
return "High Card"
|
This code checks the given ranks and suits of the cards to determine the best possible hand in poker, returning the corresponding string value.