|
| 1 | +package in.knowledgegate.dsa.maps; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * You're creating a game with some amusing mini-games, |
| 8 | + * and you've decided to make a simple variant of |
| 9 | + * the game Mahjong. |
| 10 | + * |
| 11 | + * In this variant, players have a number of tiles, |
| 12 | + * each marked 0-9. The tiles can be grouped into |
| 13 | + * pairs or triples of the same tile. For example, |
| 14 | + * if a player has "33344466", the player's hand |
| 15 | + * has a triple of 3s, a triple of 4s, and a pair |
| 16 | + * of 6s. Similarly, "55555777" has a triple of 5s, |
| 17 | + * a pair of 5s, and a triple of 7s. |
| 18 | + * |
| 19 | + * A "complete hand" is defined as a collection |
| 20 | + * of tiles where all the tiles can be grouped |
| 21 | + * into any number of triples (zero or more) and |
| 22 | + * exactly one pair, and each tile is used in |
| 23 | + * exactly one triple or pair. |
| 24 | + * |
| 25 | + * Write a function that takes a string representation of a collection of tiles in no particular order, and returns true or false depending on whether or not the collection represents a complete hand. |
| 26 | + * |
| 27 | + * tiles1 = "11133555" # True. 111 33 555 |
| 28 | + * tiles2 = "111333555" # False. There are three triples, 111 333 555 but no pair. |
| 29 | + * tiles3 = "00000111" # True. 000 00 111. Your pair and a triplet can be of the same value |
| 30 | + * # There is also no limit to how many of each tile there is. |
| 31 | + * tiles4 = "13233121" # True. Tiles are not guaranteed to be in order |
| 32 | + * tiles5 = "11223344555" # False. There cannot be more than one pair |
| 33 | + * tiles6 = "99999999" # True. You can have many of one tile |
| 34 | + * tiles7 = "999999999" # False. |
| 35 | + * tiles8 = "9" # False. |
| 36 | + * tiles9 = "99" # True. One pair. |
| 37 | + * tiles10 = "000022" # False. |
| 38 | + * tiles11 = "888889" # False. There cannot be any tiles left over. |
| 39 | + * tiles12 = "889" # False. There cannot be any tiles left over. |
| 40 | + * tiles13 = "88888844" # True. Two triples and one pair |
| 41 | + * tiles14 = "77777777777777" # True. Four triples and one pair |
| 42 | + * tiles15 = "1111111" # False. |
| 43 | + * tiles16 = "1111122222" # False. |
| 44 | + * |
| 45 | + * complete(tiles1) => True |
| 46 | + * complete(tiles2) => False |
| 47 | + * complete(tiles3) => True |
| 48 | + * complete(tiles4) => True |
| 49 | + * complete(tiles5) => False |
| 50 | + * complete(tiles6) => True |
| 51 | + * complete(tiles7) => False |
| 52 | + * complete(tiles8) => False |
| 53 | + * complete(tiles9) => True |
| 54 | + * complete(tiles10) => False |
| 55 | + * complete(tiles11) => False |
| 56 | + * complete(tiles12) => False |
| 57 | + * complete(tiles13) => True |
| 58 | + * complete(tiles14) => True |
| 59 | + * complete(tiles15) => False |
| 60 | + * complete(tiles16) => False |
| 61 | + * |
| 62 | + * Complexity Variable |
| 63 | + * N - Number of tiles in the input string |
| 64 | + */ |
| 65 | +public class MiniMahjongWinningHand { |
| 66 | + public boolean isWinningHand(String hand) { |
| 67 | + Map<Character, Integer> map = new HashMap<>(); |
| 68 | + for (int i = 0; i < hand.length(); i++) { |
| 69 | + char c = hand.charAt(i); |
| 70 | + map.put(c, map.getOrDefault(c, 0) + 1); |
| 71 | + } |
| 72 | + |
| 73 | + boolean pairFound = false; |
| 74 | + for (Integer count : map.values()) { |
| 75 | + if (count % 3 == 2) { |
| 76 | + if (!pairFound) { |
| 77 | + pairFound = true; |
| 78 | + } else { |
| 79 | + return false; |
| 80 | + } |
| 81 | + } else if (count % 3 != 0) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + return pairFound; |
| 86 | + } |
| 87 | + |
| 88 | + public static void main(String[] args) { |
| 89 | + String tiles1 = "11133555"; |
| 90 | + String tiles2 = "111333555"; |
| 91 | + String tiles3 = "00000111"; |
| 92 | + String tiles4 = "13233121"; |
| 93 | + String tiles5 = "11223344555"; |
| 94 | + String tiles6 = "99999999"; |
| 95 | + String tiles7 = "999999999"; |
| 96 | + String tiles8 = "9"; |
| 97 | + String tiles9 = "99"; |
| 98 | + String tiles10 = "000022"; |
| 99 | + String tiles11 = "888889"; |
| 100 | + String tiles12 = "889"; |
| 101 | + String tiles13 = "88888844"; |
| 102 | + String tiles14 = "77777777777777"; |
| 103 | + String tiles15 = "1111111"; |
| 104 | + String tiles16 = "1111122222"; |
| 105 | + MiniMahjongWinningHand obj = |
| 106 | + new MiniMahjongWinningHand(); |
| 107 | + System.out.println(tiles1 + ":" + obj.isWinningHand(tiles1)); |
| 108 | + System.out.println(tiles2 + ":" + obj.isWinningHand(tiles2)); |
| 109 | + System.out.println(tiles3 + ":" + obj.isWinningHand(tiles3)); |
| 110 | + System.out.println(tiles4 + ":" + obj.isWinningHand(tiles4)); |
| 111 | + System.out.println(tiles5 + ":" + obj.isWinningHand(tiles5)); |
| 112 | + System.out.println(tiles6 + ":" + obj.isWinningHand(tiles6)); |
| 113 | + System.out.println(tiles7 + ":" + obj.isWinningHand(tiles7)); |
| 114 | + System.out.println(tiles8 + ":" + obj.isWinningHand(tiles8)); |
| 115 | + System.out.println(tiles9 + ":" + obj.isWinningHand(tiles9)); |
| 116 | + System.out.println(tiles10 + ":" + obj.isWinningHand(tiles10)); |
| 117 | + System.out.println(tiles11 + ":" + obj.isWinningHand(tiles11)); |
| 118 | + System.out.println(tiles12 + ":" + obj.isWinningHand(tiles12)); |
| 119 | + System.out.println(tiles13 + ":" + obj.isWinningHand(tiles13)); |
| 120 | + System.out.println(tiles14 + ":" + obj.isWinningHand(tiles14)); |
| 121 | + System.out.println(tiles15 + ":" + obj.isWinningHand(tiles15)); |
| 122 | + System.out.println(tiles16 + ":" + obj.isWinningHand(tiles16)); |
| 123 | + } |
| 124 | +} |
0 commit comments