Numeric Poker Operations
Learn this problemProblem 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:
- five of a kind
- four of a kind
- full house
- two pair
- three of a kind
- one pair
- 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 returnsFIRST,SECOND, orTIE.["BEST", partial]appends ranks untilpartialhas length five and returns the strongest possible completed hand.["WORST", partial]appends ranks untilpartialhas 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
1through9. COMPAREreceives two strings of length5.BESTandWORSTreceive one string whose length is between1and5, inclusive.- Each appended rank may independently be any digit from
1through9.