TopCoder problem "DifferentPokerHands" used in TCO07 Semi 1 (Division I Level Two)



Problem Statement

    

In movies involving Poker, very often one deal (usually the last) will involve all players having good, increasingly stronger poker hands. In Texas Hold'em poker, this is a bit more challenging for the movie director since each player only has two cards for himself.

In Texas Hold'em, five community cards are dealt on the board (available to any player), and each player is given two hole cards (which can only be used by the player). When the betting is over (which need not concern us here), the player who can create the best five card poker hand using the seven cards he has available (his own hole cards, and the community cards), wins.

Below follows a list of different rankings, from the worst to the best. Note that all five cards may be needed in order to compare two different hands, in case the hands have the same ranking. If a hand satisfies several rankings, only the best one is considered.

  • High Card - If you don't have any of the hands below, your highest card determines your hand. When comparing with another High Card hand, the highest card in the two hands are first compared. If there is a tie, the second highest card in each hand is compared, and so on. Example: KS JD TC 8D 2H
  • One Pair - Two cards of the same rank. Pairs with higher rank beat lower pairs. In case of a tie, the remaining three cards are used as tie-breakers, compared in descending order of rank (as in High Card). Example: 7S 7H JH 9D 3C
  • Two Pairs - Two cards of the same rank, plus another two cards of a different rank. In case of a tie, the highest pair is first compared, then the second pair, and finally the rank of the fifth remaining card. Example: QH QD TH TC 5S
  • Three of a Kind - Three cards of the same rank. In case of a tie, the highest rank of the three-of-a-kind is first considered. In case of a tie, the remaining two cards are used as tie-breakers, compared in descending order. Example: 4H 4S 4D KC 7C
  • Straight - Five cards in consecutive rank. An Ace can either be accounted as high (14) or low (1), but not both. Hence wrapping is not allowed. Two straights are compared using the highest card (in case of A, 2, 3, 4, 5, the highest card is 5). Example: JH TC 9S 8S 7H
  • Flush - Five cards of the same suit. Since all suits have equal value, when comparing two flushes, the highest card is first considered, then the second highest and so on (as in High Card). Example: AD QD 9D 7D 2D
  • Full House - Three cards of the same rank, and two cards of another rank. In case of a tie, compare the rank of the three cards first, then the two cards. Example: 6D 6S 6C JC JS
  • Four of a Kind - Four cards of the same rank. In case of a tie, the fifth card is used as a tie-breaker. Example: 8S 8D 8C 8H KS
  • Straight Flush - Five cards in consecutive rank that also are of the same suit. Same tie-breaker as for a straight. Example: TD 9D 8D 7D 6D
  • Royal Straight Flush - A special case of Straight Flush where the highest card is an Ace. All four royal straight flushes have the same value. Example: AC KC QC JC TC

Given the five community cards, determine the number of poker hands that are evaluated differently that can be constructed using different pairs of hole cards. For each pair of hole cards, only one poker hand counts (the one with the best ranking). Create a class DifferentPokerHands containing the method noDifferentHands which takes as input a String[] communityCards, containing the five community cards, and returns an int, the number of different poker hands that can be constructed. Each community card is given in the format (quotes for clarity) "<rank><suit>" where <rank> is in {'2'-'9', 'T', 'J', 'Q', 'K', 'A'} and <suit> is in {'H', 'S', 'D', 'C'}.

 

Definition

    
Class:DifferentPokerHands
Method:noDifferentHands
Parameters:String[]
Returns:int
Method signature:int noDifferentHands(String[] communityCards)
(be sure your method is public)
    
 

Notes

-The different hands may overlap. See example 0.
 

Constraints

-communityCards will contain exactly 5 elements.
-Each element in communityCards will be formatted as described in the statement.
-All elements in communityCards will be distinct.
 

Examples

0)
    
{"5S", "6S", "7S", "8S", "9S"}
Returns: 3
The five community cards alone form a straight flush, so any pair of hole cards will be a straight flush with 9 on top. But if one of the hole cards is a ten of spades, the player will have a stronger straight flush. If the two hole cards are a ten of spades and a jack of spades, the straight flush will be even stronger. So in total there are three different hands that can be constructed.
1)
    
{"AS", "AD", "KH", "KC", "QS"}
Returns: 7
The different hands that can be constructed are, in descending order of rank (some of them can be constructed in more than one way):
AS AD AC AH KH - Four of a Kind, Aces with king as a tie-breaker card (AS AD AC AH KC would have the same rank)
KH KC KD KS AS - Four of a Kind, kings with Ace as a tie-breaker card
AS AD AC KH KC - Full House, Aces and kings
AS AD KH KC KD - Full House, kings and Aces
AS AD QS QC QD - Full House, queens and Aces
AS KH QS JC TC - Straight, Ace on top
AS AD KH KC QS - Two Pairs, Aces and kings, with a queen as a tie-breaker card
2)
    
{"AH", "8S", "6S", "4S", "AS"}
Returns: 96
The five community cards from the last deal in "Casino Royale".

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7769

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10839&pm=7769

Writer:

Yarin

Testers:

PabloGilberto , Olexiy , ivan_metelsky

Problem categories:

Brute Force