Problem · String

Numeric Poker Operations

Learn this problem
MediumRippling logoRipplingFULLTIMEPHONE SCREEN

Problem statement

A numeric poker hand is a string of exactly five card ranks. Every rank is a digit from 1 through 9, and ranks may be reused without a deck-wide limit.

Hand Strength

Categories are ordered from strongest to weakest:

  1. five of a kind
  2. four of a kind
  3. full house
  4. two pair
  5. three of a kind
  6. one pair
  7. high card

A stronger category always wins. When two hands have the same category, compare their original card order from right to left without sorting. At the first different position, the hand with the larger rank is stronger. Identical hands tie.

Process each row of operations in order and return one string result per row:

  • ["COMPARE", first, second] compares two complete hands and returns FIRST, SECOND, or TIE.
  • ["BEST", partial] appends ranks until partial has length five and returns the strongest possible completed hand.
  • ["WORST", partial] appends ranks until partial has length five and returns the weakest possible completed hand.

Every completion preserves the given prefix. If the input to BEST or WORST already has length five, return it unchanged.

Function

processNumericPokerOperations(operations: String[][]) → String[]

Examples

Example 1

operations = [["COMPARE","99999","88888"],["COMPARE","11223","11123"],["COMPARE","12345","12345"]]return = ["FIRST","FIRST","TIE"]

The first result compares two five-of-a-kind hands by their ranks. In the second comparison, two pair is listed above three of a kind in this game's category order. The last hands are identical.

Example 2

operations = [["BEST","99"],["WORST","99"],["BEST","12345"],["WORST","12345"]]return = ["99999","99321","12345","12345"]

Appending three nines produces the strongest completion of 99. Every completion of that prefix has at least one pair, and 99321 is the weakest one-pair completion under right-to-left comparison. Complete hands remain unchanged.

Example 3

operations = [["COMPARE","12349","92341"],["BEST","1"],["WORST","1"],["BEST","1234"],["WORST","1234"]]return = ["FIRST","11111","15432","12344","12345"]

The first comparison is decided by the rightmost card, so the leading 9 in the second hand does not decide the result. The completion results follow the same category and right-to-left ordering.

Constraints

  • 1 <= operations.length <= 2000.
  • Every operation is exactly one valid shape listed above.
  • Every hand or partial hand contains only digits from 1 through 9.
  • COMPARE receives two strings of length 5.
  • BEST and WORST receive one string whose length is between 1 and 5, inclusive.
  • Each appended rank may independently be any digit from 1 through 9.

More Rippling problems

drafts saved locally
public String[] processNumericPokerOperations(String[][] operations) {
    // Write your code here.
}
operations[["COMPARE","99999","88888"],["COMPARE","11223","11123"],["COMPARE","12345","12345"]]
expected["FIRST", "FIRST", "TIE"]
checking account