FastPrepValidate a Mahjong Hand Partition

Validate a Mahjong Hand Partition

Duolingo logoDuolingo● MediumNEW GRADPHONE SCREEN
Learn

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[]) → boolean

Examples

Example 1

tiles = [1,1,1,2,3,4,5,6]return = true

Use [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 = true

The hand partitions into the triple [1,1,1] and the pair [2,2].

Example 3

tiles = [1,1,1,1,2]return = false

After 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 1 and 9.
  • A necessary valid-hand length has the form 3g + 2 for some g >= 0.

More Duolingo problems

See Duolingo hiring insights
public boolean isValidMahjongHand(int[] tiles) {
    // write your code here
}
tiles[1,1,1,2,3,4,5,6]
expectedtrue
Checking account…