call(back)
Algorithms & Data StructureseasySign in to star problems

Classify a Poker Hand

Classify a five-card poker hand. Each card is a string: the rank (A, 2–10, J, Q, K) followed by the suit (S, H, D, C). Return the best category that applies:

CategoryMeaning
Straight flusha straight, all one suit
Four of a kindfour cards of one rank
Full housethree of one rank and two of another
Flushall one suit
Straightfive consecutive ranks
Three of a kindthree cards of one rank
Two pairtwo pairs
One pairtwo cards of one rank
High cardnone of the above

An ace plays high or low in a straight — 10 J Q K A and A 2 3 4 5 both count — but a straight never wraps around: Q K A 2 3 is not one. The five cards are distinct.

handCategory(["10H", "JH", "QH", "KH", "AH"])  ->  "Straight flush"
handCategory(["2C", "2D", "9H", "9S", "KC"])   ->  "Two pair"

Hints

Hint 1

Parse the rank as everything but the last character — 10H is the one three-character card — and map A, J, Q, K to 1, 11, 12, 13.

Hint 2

Count each rank and sort the counts descending: [4, 1] is four of a kind, [3, 2] a full house, [3, 1, 1] three of a kind, [2, 2, 1] two pair, [2, 1, 1, 1] one pair.

Hint 3

A flush is one distinct suit; a straight is five distinct values spanning exactly 4, plus the ace-high case A 10 J Q K. Check the categories best first and return the first that applies.

Asked at