Quick 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.

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

  1. Only cards of the same suit can belong to one straight flush, so consider each suit separately.
  2. A card's rank is everything after its first character, and the rank 10 takes two characters.
  3. 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.

Loading coding console...

Show the approach

Approach

Algorithm: read each card's suit (first character) and rank (everything after it, so the two-character rank 10 is parsed as ten), numbering A=1, 2-10 by face value, J=11, Q=12 and K=13. Record, per suit, which rank values are present; an ace is recorded both as 1 and as 14 so it can sit below the 2 or above the king. Then, for each suit, test every window of five values start, start+1, ..., start+4 with start from 1 to 10 and return True as soon as one window is fully present; otherwise return False.

Invariant and correctness: after the scan, a suit's presence table holds exactly the ranks of that suit in the hand (cards are distinct, so presence is all that matters). Five same-suit cards with consecutive ranks in the allowed order are exactly one of the ten windows: start 1 is A-2-3-4-5 (ace low), starts 2 through 9 are the ordinary runs, and start 10 is 10-J-Q-K-A (ace high). A wrap such as Q-K-A-2-3 or K-A-2-3-4 would need the ace in the middle of a run, i.e. values past 14, so no window matches it. Cards of other suits never enter a suit's table, so equal ranks in different suits cannot combine, and extra cards outside a window neither help nor hurt.

Edge cases: exactly five cards; extra off-suit cards in any order; the same ranks spread across suits; a suit with five or more cards but a gap; a suit with only four consecutive ranks; runs of six or more in one suit; the full 52-card deck.

Time complexity:
O(n)
Space complexity:
O(1)