Find All Zip Rummy Melds
Problem statement
Zip Rummy uses a 36-card deck. Each card has a rank from 1 to 9 and one suit: clubs (C), diamonds (D), hearts (H), or spades (S). You receive one 10-card hand as strings such as "7C".
Find every meld of at least three cards that can be formed from this hand:
- A set contains cards of the same rank with different suits.
- A run contains cards of consecutive ranks in the same suit.
Return all possible sets and runs, including shorter melds contained in longer ones. List cards within a set in suit order C, D, H, S, and cards within a run in increasing rank order. Return sets first in increasing rank order. Within one rank, return shorter sets before longer sets and then lexicographic suit order. Return runs next in increasing length, then starting rank, then suit order.
Function
findMelds(hand: String[]) → String[][]Examples
Example 1
hand = ["1C","9C","8C","7C","5C","4C","9D","9S","1D","1H"]return = [["1C","1D","1H"],["9C","9D","9S"],["7C","8C","9C"]]The hand has two three-card sets and one three-card run in clubs.
Example 2
hand = ["4C","2C","3C","1C","8D","6C","9D","9C","1D","1H"]return = [["1C","1D","1H"],["1C","2C","3C"],["2C","3C","4C"],["1C","2C","3C","4C"]]The four consecutive clubs make two runs of length three and one run of length four.
Constraints
hand.length == 10.- Every card is unique and belongs to the 36-card deck described above.