Mahjong Winning Hand
Asked at Google
Reported by the problem's creator, not verified
A mahjong hand is 14 tiles. Each tile is a string such as "3m", "7p" or "1s": a rank from 1 to 9 followed by a suit letter m, p or s. There are four copies of each tile, so a hand never holds more than four of the same tile. A hand wins when its 14 tiles can be split into one pair (two identical tiles) and four melds. A meld is either a triplet (three identical tiles) or a run (three tiles of the same suit with consecutive ranks, such as 4p 5p 6p). A run does not wrap around, so 8m 9m 1m is not a run. No other winning shape counts, including seven pairs.
Examples
Example 1
Input: tiles =
["1m","1m","1m","2m","3m","4m","5p","6p","7p","9s","9s","9s","3p","3p"]Output:
trueExplanation: The pair is
3p 3p; the rest split into the triplet1m 1m 1m, the runs2m 3m 4mand5p 6p 7p, and the triplet9s 9s 9s.Example 2
Input: tiles =
["1m","1m","1m","2m","3m","4m","5p","6p","7p","9s","9s","9s","3p","4p"]Output:
falseExplanation: The stray tiles
3p 4pfit no pair and no meld, so no split into one pair and four melds exists.