I was on a interview with an algorithm problem, which I cannot seem to solve nor find a solution already for it, but I'm really curious how it can be solved.
Here is the problem.
You’re working on making some mini-games, and you’ve decided to make a simpler variant of the game Mahjong. In this variant, players have a number of tiles, each marked 0-9. The tiles can be grouped into pairs or triples of the same tile. A “complete hand” is defined as a collection of tiles where all the tiles can be grouped into any number of triples (zero or more) and exactly one pair, and each tile is used in exactly one triple or pair. Write a function that takes a string representing a player’s tiles in no particular order, and returns the string “COMPLETE” if the hand is complete, and “NOT COMPLETE” if it is not. Each tile may only be used in a single group (either a pair or a triple). All tiles must be used to complete a hand. There may be a pair and triples of the same tile. A single pair with no triples (ex: 99) is a valid hand. The input will only consist of characters 0-9.
These three hands would all return “COMPLETE”:
33344466 -> 333 444 66 This hand can be grouped into a triple of 3s and 4s, and a pair of 6s
55555555 -> 555 555 55 This hand can be grouped into two triples of 5s and a pair of 5s
22 -> 22 This hand is a single pair of 2s (no triples are required)
These three hands would all return “NOT COMPLETE”:
1357 This hand has tiles which cannot be formed into pairs or triples
335577 This hand has three pairs, a complete hand must have exactly one pair
666888 This hand has two triples, but no pairs.
Is this related to some math formula or some already know algorithm?