LeetCode - Algorithms - 2347. Best Poker Hand

Problem

2347. Best Poker Hand

Python

Counter

1
2
3
4
5
6
7
8
9
10
11
12
13
from collections import Counter

class Solution:
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
c = Counter(ranks).values()
if all(suits[0] == suit for suit in suits):
return "Flush"
elif max(c) > 2:
return "Three of a Kind"
elif max(c) == 2:
return "Pair"
else:
return "High Card"

Submission Detail

  • 98 / 98 test cases passed.
  • Runtime: 58 ms, faster than 42.46% of Python3 online submissions for Best Poker Hand.
  • Memory Usage: 13.8 MB, less than 97.47% of Python3 online submissions for Best Poker Hand.

Dictionary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
d = dict()
for rank in ranks:
if rank not in d:
d[rank] = 1
else:
d[rank] += 1
c = d.values()
if all(suits[0] == suit for suit in suits):
return "Flush"
elif max(c) > 2:
return "Three of a Kind"
elif max(c) == 2:
return "Pair"
else:
return "High Card"

Submission Detail

  • 98 / 98 test cases passed.
  • Runtime: 71 ms, faster than 7.61% of Python3 online submissions for Best Poker Hand.
  • Memory Usage: 13.9 MB, less than 68.49% of Python3 online submissions for Best Poker Hand.