Detect Whether Any Five Playing Cards Form a Straight Flush
Company: Temu
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
You are given an array of strings `cards` describing playing cards taken from one standard 52-card deck. Each card is written as its suit letter followed by its rank, for example `CA` (ace of clubs), `H10` (ten of hearts), `SK` (king of spades) and `D5` (five of diamonds). Determine whether some 5 of the given cards form a straight flush: five cards of the same suit whose ranks are consecutive.
### Function Signature
```python
def has_straight_flush(cards: list[str]) -> bool:
```
### Rules
- The first character of a card is its suit: `S` (spades), `H` (hearts), `C` (clubs) or `D` (diamonds). The remaining one or two characters are its rank: `A`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `J`, `Q` or `K`.
- Ranks in order are `A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K`, and the ace may also count as the highest rank, directly above the king.
- Five cards form a straight flush when all five have the same suit and their ranks are five consecutive ranks in that order. Both `A-2-3-4-5` and `10-J-Q-K-A` qualify; a run that wraps around through the ace, such as `Q-K-A-2-3` or `K-A-2-3-4`, does not.
- Return `True` if at least one set of 5 cards from `cards` forms a straight flush, otherwise `False`. Cards that are not part of the straight flush do not matter.
### Constraints
- `5 <= len(cards) <= 52`
- Every card string is well formed as described above, uses uppercase letters, and is 2 or 3 characters long.
- All cards are distinct, as in a single deck.
- The order of `cards` is arbitrary.
### Examples
**Example 1**
```text
Input: cards = ["H10", "HJ", "C2", "HQ", "HK", "HA"]
Output: True
```
The hearts 10, J, Q, K and A form a straight flush with the ace counted high. The two of clubs is ignored.
**Example 2**
```text
Input: cards = ["S3", "SA", "D4", "S5", "S2", "S4"]
Output: True
```
The spades A, 2, 3, 4 and 5 form a straight flush with the ace counted low. The four of diamonds is ignored.
**Example 3**
```text
Input: cards = ["DQ", "DK", "DA", "D2", "D3", "H4", "H5", "C6", "H7", "H8"]
Output: False
```
The diamonds Q, K, A, 2 and 3 would wrap around through the ace, which is not allowed. The ranks 4 through 8 are consecutive but mix hearts and clubs, and only four hearts are present.
Overview: Given 5 to 52 distinct playing cards written as suit-plus-rank strings such as H10 or SK, decide whether any five of them form a straight flush. The task tests careful parsing of two- and three-character card codes and treating the ace as both the lowest and the highest rank without allowing runs that wrap around.
You are given an array of strings `cards` describing playing cards taken from one standard 52-card deck. Each card is written as its suit letter followed by its rank, for example `CA` (ace of clubs), `H10` (ten of hearts), `SK` (king of spades) and `D5` (five of diamonds). Determine whether some 5 of the given cards form a **straight flush**: five cards of the same suit whose ranks are consecutive.
### Rules
- The first character of a card is its suit: `S` (spades), `H` (hearts), `C` (clubs) or `D` (diamonds). The remaining one or two characters are its rank: `A`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `J`, `Q` or `K`.
- Ranks in order are `A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K`, and the ace may also count as the highest rank, directly above the king.
- Five cards form a straight flush when all five have the same suit and their ranks are five consecutive ranks in that order. Both `A-2-3-4-5` and `10-J-Q-K-A` qualify; a run that wraps around through the ace, such as `Q-K-A-2-3` or `K-A-2-3-4`, does not.
- Return `True` (`true` in JavaScript, Java and C++) if at least one set of 5 cards from `cards` forms a straight flush, otherwise `False` (`false`). Cards that are not part of the straight flush do not matter.
### Example 1
```text
Input: cards = ["H10", "HJ", "C2", "HQ", "HK", "HA"]
Output: True
```
The hearts 10, J, Q, K and A form a straight flush with the ace counted high. The two of clubs is ignored.
### Example 2
```text
Input: cards = ["DQ", "DK", "DA", "D2", "D3", "H4", "H5", "C6", "H7", "H8"]
Output: False
```
The diamonds Q, K, A, 2 and 3 would wrap around through the ace, which is not allowed. The ranks 4 through 8 are consecutive but mix hearts and clubs, and only four hearts are present.
### Constraints
- `5 <= len(cards) <= 52`
- Every card string is well formed as described above, uses uppercase letters, and is 2 or 3 characters long.
- All cards are distinct, as in a single deck.
- The order of `cards` is arbitrary.
Constraints
- 5 <= len(cards) <= 52
- Every card string is well formed as described above, uses uppercase letters, and is 2 or 3 characters long.
- All cards are distinct, as in a single deck.
- The order of cards is arbitrary.
Examples
Input: (['H10', 'HJ', 'C2', 'HQ', 'HK', 'HA'],)
Expected Output: True
Explanation: Source example 1: hearts 10-J-Q-K-A with the ace counted high; the two of clubs is ignored. 6 cards.
Input: (['S3', 'SA', 'D4', 'S5', 'S2', 'S4'],)
Expected Output: True
Explanation: Source example 2: spades A-2-3-4-5 with the ace counted low; the four of diamonds is ignored. 6 cards.
Hints
- Only cards of the same suit can belong to one straight flush, so consider each suit separately.
- A card's rank is everything after its first character, and the rank 10 takes two characters.
- The ace can sit at either end of a run (A-2-3-4-5 or 10-J-Q-K-A), but a run may never continue from K through A to 2.