Consider a deck of normal playing cards.
A playing card is represented as a string, where the first part represents the rank (one of Ace A, 2, 3, …, 10, Jack J, Queen Q, King K) and the second part represents the suit (one of Clubs C, Diamonds D, Hearts H, Spades S).
A valid set of playing cards is a set of playing cards that fulfills one of the two following criteria:
Criterion 1: the set has 3 or more cards and all cards in the set have the same rank and any suit. Examples:
"AC","AD","AS"(i.e. Ace of Clubs, Diamonds and Spades)"5C","5D","5H","5S"(i.e. Five of Clubs, Diamonds, Spades and Hearts)
Criterion 2: the set has 3 or more cards and all cards in the set have the same suit and consecutive ascending ranks. Examples:
"2C","3C","4C"(i.e. 2, 3 and 4 of Clubs)"9H","10H","JH","QH","KH"(i.e. 9, 10, Jack, Queen and King of Hearts)
Write a function that takes as input a list of cards, and determines if the input is a valid playing card set.
这道题要求判断一组扑克牌是否构成“有效牌组”。规则只有两类:第一类是所有牌点数相同、花色可以不同,但数量至少 3 张;第二类是所有牌花色相同,且点数必须是连续递增的,数量也至少 3 张。解题时通常先把每张牌拆成 rank 和 suit,再分别检查是否满足“同点数”或“同花色 + 连续”的条件。实现上可以用集合统计花色与点数,也可以先把 rank 映射成数值后排序判断相邻差值是否为 1。