1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Sample121 {
    private static final String RANK_LIST = "A23456789TJQKA";

    private static final String ROYAL = "royal ";
    private static final String STRAIGHT = "straight";
    private static final String FLUSH = "flush";
    private static final String FULL_HOUSE = "Full house";
    private static final String CARDS_4 = "Four of a kind";
    private static final String CARDS_3 = "Three of a kind";
    private static final String PAIR_2 = "Two pair";
    private static final String PAIR_1 = "One pair";
    private static final String Nothing = "No pair";

    public static String getPokerRole(String cards) {
        if (cards.length() != 2 * 5) throw new IllegalArgumentException();
        String rankList = createRankList(cards);
        boolean flush = isFlush(cards);
        boolean straight = isStraight(rankList);
        if (flush || straight) {
            boolean royal = isRoyal(rankList);
            if (royal) {
                return format(flush? ROYAL + FLUSH: ROYAL + STRAIGHT);
            } else {
                if (flush && straight) {
                    return format(STRAIGHT + " " + FLUSH);
                } else {
                    return format(flush? FLUSH: STRAIGHT);
                }
            }
        }
        Integer[] integers = countSameNumber(rankList);
        switch (integers[0]) {
            case 4:
                return CARDS_4;
            case 3:
                if (integers[1] == 2) {
                    return FULL_HOUSE;
                } else {
                    return CARDS_3;
                }
            case 2:
                if (integers[1] == 2) {
                    return PAIR_2;
                } else {
                    return PAIR_1;
                }
        }
        return Nothing;
    }

    private static String format(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    private static boolean isFlush(String cards) {
        char suit = cards.charAt(0);
        for (int index = 1; index < 5; index++) {
            if (suit != cards.charAt(index * 2)) {
                return false;
            }
        }
        return true;
    }

    private static String createRankList(String cards) {
        StringBuilder builder = new StringBuilder();
        for (int index = 0; index < 5; index++) {
            final char rank = cards.charAt(index * 2 + 1);
            int insert = 0;
            for (; insert < builder.length(); insert++) {
                if (RANK_LIST.indexOf(builder.charAt(insert)) > RANK_LIST.indexOf(rank)) {
                    break;
                }
            }
            builder.insert(insert, rank);
        }
        return builder.toString();
    }
    private static boolean isStraight(String rankList) {
        return RANK_LIST.indexOf(rankList) >= 0;
    }
    private static boolean isRoyal(String rankList) {
        return rankList.equals("ATJQK");
    }

    private static Integer[] countSameNumber(String rankList) {
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (int index = 0; index < rankList.length(); index++) {
            char c = rankList.charAt(index);
            Integer integer = map.get(c);
            if (integer == null) {
                integer = 1;
                map.put(c, integer);
            } else {
                map.put(c, integer + 1);
            }
        }
        List<Integer> result = new ArrayList<Integer>(map.values());
        Collections.sort(result, Collections.reverseOrder());
        return result.toArray(new Integer[0]);
    }


    public static void main(String[] args) {
        System.out.println(getPokerRole("SQSJSASKST"));
        System.out.println(getPokerRole("D9D7D6D5D8"));
        System.out.println(getPokerRole("C2D2S2H3H2"));
        System.out.println(getPokerRole("C2D3S2H3H2"));
        System.out.println(getPokerRole("S9S4S8STSJ"));
        System.out.println(getPokerRole("C4H7D5S6H3"));
        System.out.println(getPokerRole("S6H6C5DQC6"));
        System.out.println(getPokerRole("S6HQC5DQC6"));
        System.out.println(getPokerRole("S6H4C5DQC6"));
        System.out.println(getPokerRole("SJSQSKSAC2"));
    }
}