Problem · Design
MediumRipplingFULLTIMEPHONE SCREEN

Problem statement

Camel Cards is a simplified two-player card game. You are given two strings, hand1 and hand2, representing the players' four-card hands. Each card is labeled with a digit from 1 through 9, where 9 is highest.

Hand Types

From strongest to weakest:

  1. Four of a kind: all four cards are equal, such as 9999.
  2. Two pair: two cards of one value and two cards of another value, such as 2332.
  3. Three of a kind: three equal cards and one different card, such as 9998.
  4. One pair: two equal cards and two other distinct cards, such as 5233.
  5. High card: all four cards are distinct, such as 2345.

Ordering Rules

  • A stronger hand type always wins.
  • If both hands have the same type, compare their cards from right to left, which is from most recently dealt to first dealt.
  • Do not sort the cards. At the first differing position, the hand with the higher card wins.
  • If all four cards are equal, the result is a tie.

Return "HAND_1" if hand1 wins, "HAND_2" if hand2 wins, or "TIE".

Design the hand-type evaluation so that more hand types can be added later.

Function

evaluate(hand1: String, hand2: String) → String

Examples

Example 1

hand1 = "2332"hand2 = "2442"return = "HAND_2"

Both hands are two pair. Comparing from right to left, the final cards are tied at 2, then hand2 has 4 while hand1 has 3.

Constraints

  • hand1.length() = hand2.length() = 4
  • Every character in hand1 and hand2 is a digit from 1 through 9.

More Rippling problems

drafts saved locally
public String evaluate(String hand1, String hand2) {
  // write your code here
}
hand1"2332"
hand2"2442"
expected"HAND_2"
checking account