forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestPokerHand.java
42 lines (35 loc) · 1.08 KB
/
BestPokerHand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// https://leetcode.com/problems/best-poker-hand
// T: O(1)
// S: O(1)
import java.util.HashMap;
import java.util.Map;
public class BestPokerHand {
public String bestHand(int[] ranks, char[] suits) {
if (sameSuit(suits)) {
return "Flush";
}
final Map<Integer, Integer> frequencies = getFrequencies(ranks);
final int maxFrequency = frequencies.values().stream().max(Integer::compare).get();
if (maxFrequency >= 3) {
return "Three of a Kind";
}
if (maxFrequency == 2) {
return "Pair";
}
return "High Card";
}
private Map<Integer, Integer> getFrequencies(int[] ranks) {
final Map<Integer, Integer> frequencies = new HashMap<>();
for (int rank : ranks) {
frequencies.put(rank, frequencies.getOrDefault(rank, 0) + 1);
}
return frequencies;
}
private boolean sameSuit(char[] suits) {
char first = suits[0];
for (char suit : suits) {
if (suit != first) return false;
}
return true;
}
}