You are given an array of strings cards, where each string represents a distinct playing card. Each card string has the format <rank><suit>:
rank
is an integer from 1 to 13 (you can assume it is written without leading zeros, e.g.,
1
,
10
,
13
).
suit
is a single uppercase letter from the set
{C, D, H, S}
(Clubs, Diamonds, Hearts, Spades).
Example of input:
We define a valid group of cards as any set of cards that satisfies either of the following conditions:
Additional requirement for same-suit consecutive runs:
L ≥ 3
(i.e., you cannot extend it on either side within that suit), you must output
every contiguous sub-run
whose length is between 3 and
L
, inclusive.
C
you have ranks
[1,2,3,4,5,6]
, then you must output all of these runs (written here as ranks only):
[1,2,3]
,
[2,3,4]
,
[3,4,5]
,
[4,5,6]
[1,2,3,4]
,
[2,3,4,5]
,
[3,4,5,6]
[1,2,3,4,5]
,
[2,3,4,5,6]
[1,2,3,4,5,6]
For same-rank sets:
k ≥ 3
times (with any suits), you should output
one group
containing
all
cards of that rank.
A card group that satisfies both properties (for example, three cards of the same suit and in consecutive ranks) should still be output only once as a single group.
The result should be all such valid groups, represented as lists of card strings. The order of groups in the output and the order of cards within each group do not matter.
Example
Input:
Valid groups are:
C
, ranks 1,2,3):
["1C", "2C", "3C"]
C
,
H
,
D
):
["1C", "1H", "1D"]
So one possible output is:
Task: Implement a function that, given cards, returns all valid groups as described above. Provide the algorithm and code to compute the output.