Decide whether a hand of numbered suit tiles contains a valid group or run of at least three tiles.
Easy3ImplementationSortingHash mapInterviewNo attempts yetTime limit1sMemory limit256 MBRummikub is a simple game, often played by older people against their grandchildren. The tile set has four suits, shown by the colors blue (b), green (g), red (r) and yellow (y). Each suit holds N different tiles numbered from 1 to N, where N>0, and the number on a tile is its value. There are two tiles for every (value, suit) combination.
At the start of the game the referee deals each player a hand of tiles, and only that player sees them. Every tile that is not dealt goes face down on the table and forms the pool. Players take turns playing tiles. A tile may be played only as part of a group or a run. A group is a set of at least three tiles with the same value and all different suits. A run is a set of at least three tiles of the same suit with consecutive values. A player who cannot play any tile must take a tile from the pool.
The older players want to play many tiles per turn, and they dislike taking a tile from the pool because that stops their progress. Sometimes they take a tile anyway, after overlooking a run or a group in their own hand. Given a hand of tiles, write a program that decides whether a new run or group can be formed.
The first line contains an integer T, the number of test cases. Each test case takes two lines.
For each test case, print YES on its own line if the player can form a run or a group, and NO otherwise.