FastPrepValidate a Simplified Mahjong Winning Hand

Validate a Simplified Mahjong Winning Hand

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

tiles contains exactly fourteen tile ranks from 1 through 9. A winning hand can be partitioned into exactly four melds and one pair.

  • A pair is two equal ranks.
  • A meld is either three equal ranks or three consecutive ranks.

Every tile occurrence must be used exactly once. Return whether the hand is winning. Special Mahjong hands are not included.

Function

isWinningMahjongHand(tiles: int[]) → boolean

Examples

Example 1

tiles = [1,1,1,2,3,4,2,3,4,7,8,9,5,5]return = true

Use triple 111, runs 234, 234, and 789, plus pair 55.

Example 2

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

Use pair 11 and runs 234, 234, 567, and 567.

Example 3

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

No choice of pair lets all remaining tiles form four complete melds.

Constraints

  • tiles.length == 14.
  • 1 <= tiles[i] <= 9.
  • No rank appears more than four times.

More Google problems

See Google hiring insights
public boolean isWinningMahjongHand(int[] tiles) {
    // write your code here
}
tiles[1,1,1,2,3,4,2,3,4,7,8,9,5,5]
expectedtrue
Checking account…