You are given 13 Mahjong tiles, each represented by an integer between 1 and 9.
Each integer represents the number printed on the tile.
In Mahjong, a winning hand requires one pair (雀頭, e.g., 3 3) and four sets.
Each set consists of exactly three tiles, and can be either of the following:
Triplet (刻子) — three tiles with the same number, e.g., 4 4 4.
Sequence (順子) — three tiles with consecutive numbers, e.g., 4 5 6.
A complete Mahjong winning hand therefore contains
1 pair (2 tiles) + 4 sets (4×3 = 12 tiles) = 14 tiles.
Since we are given only 13 tiles, we are looking for which tile(s) could complete the hand —
in other words, we want to determine which tiles are winning tiles (聽牌).
Sample Explanation
Given the following 13 tiles: 1 1 1 2 2 2 3 3 3 4 4 4 4
We want to check which tile, if added, can form a valid 14-tile winning hand.
If we add 1, grouping: 11, 123, 123, 234, 444 → 1 is a winning tile
If we add 2, grouping: 111, 222, 33, 234, 444 → 2 is a winning tile
If we add 3, grouping: 111, 22, 333, 234, 444 → 3 is a winning tile
If we add 5, grouping: 111, 222, 33, 444, 345 → 5 is a winning tile
Why 4 is NOT a winning tile?
Although adding 4 would theoretically allow groupings like:
111, 222, 333, 44, 444
this would require five 4’s, which is impossible — each tile appears at most 4 times.
Therefore 4 cannot be considered a winning tile.
Try each tile 1~9 as the possible winning tile.
For each tile:
- Check that adding it does not exceed 4 copies.
- Pick every possible number as the pair.
- After removing the pair, check if the rest can form four sets
(each set is either AAA or ABC).
Output all tiles that make the hand winnable.
Thirteen integers between 1 and 9 (order does not matter).
Each integer appears at most 4 times.
All winning tiles (1 to 9), one per line, sorted in increasing order.
If no tile can complete a winning hand, output 0.
Ensure that the output formatting exactly matches the samples.