Mahjong Winning Hand

Asked at GoogleOnsite

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

  1. Example 1

    Input: tiles = ["1m","1m","1m","2m","3m","4m","5p","6p","7p","9s","9s","9s","3p","3p"]

    Output: true

    Explanation: The pair is 3p 3p; the rest split into the triplet 1m 1m 1m, the runs 2m 3m 4m and 5p 6p 7p, and the triplet 9s 9s 9s.

  2. Example 2

    Input: tiles = ["1m","1m","1m","2m","3m","4m","5p","6p","7p","9s","9s","9s","3p","4p"]

    Output: false

    Explanation: The stray tiles 3p 4p fit no pair and no meld, so no split into one pair and four melds exists.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.