Camel Cards (Simplified Poker)
Problem
Implement a simplified poker-like game called Camel Cards to determine which of two players has the stronger hand.
Each hand has exactly 4 cards and is represented as a string such as "2332" or "9998".
Card ranks are ordered from high to low as:
9, 8, 7, 6, 5, 4, 3, 2, 1
Each hand belongs to exactly one of these types, from strongest to weakest:
- Four of a kind: all four cards are the same, for example
"9999" - Two pair: two cards of one rank and two cards of another rank, for example
"2332" - Three of a kind: three cards of one rank and one different card, for example
"9998" - One pair: two cards of one rank and two distinct other cards, for example
"5233" - High card: all four cards are distinct, for example
"2345"
Implement:
evaluate(hand1: string, hand2: string) -> string
Return:
"HAND_1"ifhand1wins"HAND_2"ifhand2wins"TIE"if both hands are exactly tied
Comparison rules:
- Compare hand type first
- If both hands have the same type, compare cards from right to left
- Do not sort the cards before tie-breaking
- The first differing position determines the winner
- If all four positions match, the result is a tie
Design the solution so adding more hand types later is straightforward.
Examples
Example 1:
Input: hand1 = "2332", hand2 = "2442"
Output: "HAND_2"
Explanation:
Both hands are two pair. Compare from right to left: 2 == 2, then 3 < 4, so hand2 wins.
Example 2:
Input: hand1 = "9999", hand2 = "2332"
Output: "HAND_1"
Explanation:
Four of a kind beats two pair.
Example 3:
Input: hand1 = "2345", hand2 = "1345"
Output: "HAND_1"
Explanation:
Both are high-card hands. From right to left the last three cards tie, then 2 > 1 at the first dealt card.
Constraints
hand1.length == 4hand2.length == 4- Each character is a digit from
1to9 - Tie-breaking compares the original deal order from right to left
Solution
Sign in to get AI feedback on your answer. Your work is saved while you do.
Sign in to evaluate