Validate a Mahjong Hand Partition
Problem statement
A Mahjong-like hand is represented by an unordered array tiles whose values are digits from 1 through 9.
Return whether every tile can be partitioned into:
- exactly one pair of equal values (the pair of eyes); and
- zero or more three-tile groups, where each group is either three equal values or three consecutive values
x, x + 1, x + 2.
Every tile occurrence must belong to exactly one group. The order of tiles in the input does not matter.
Function
isValidMahjongHand(tiles: int[]) → booleanExamples
Example 1
tiles = [1,1,1,2,3,4,5,6]return = trueUse [1,1] as the pair, then form the runs [1,2,3] and [4,5,6].
Example 2
tiles = [1,1,1,2,2]return = trueThe hand partitions into the triple [1,1,1] and the pair [2,2].
Example 3
tiles = [1,1,1,1,2]return = falseAfter choosing any pair, the remaining three tiles form neither a triple nor a consecutive run.
Constraints
2 <= tiles.length <= 20.- Every tile value is between
1and9. - A necessary valid-hand length has the form
3g + 2for someg >= 0.