Skip to main content

Straight Hand in Poker

easy
Software engineer

Given a deck of standard playing cards, you randomly draw five cards. You are required to determine if these five cards can form a straight (a sequence of five consecutive values, regardless of suit).

For this problem:

  • Card values are given as strings: "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K".
  • An Ace ("A") can be treated as either 1 (to form A-2-3-4-5) or 14 (to form 10-J-Q-K-A). It cannot be used as both in the same hand, nor can it bridge a sequence (e.g., Q-K-A-2-3 is not a straight).
  • The input contains no duplicates.
  • The order of cards in the input array does not matter.

Return true if these cards can form a straight; otherwise, return false.

Example 1

Input

cards = ["A", "2", "3", "4", "5"]

Output

true

Explanation

In this case, the Ace ('A') acts as the value 1. The sequence is 1, 2, 3, 4, 5, which are five consecutive integers, forming a 'Baby Straight'.

Example 2

Input

cards = ["10", "J", "Q", "K", "A"]

Output

true

Explanation

Here, the Ace ('A') acts as 14. The sequence is 10, 11, 12, 13, 14 (Broadway Straight).

Example 3

Input

cards = ["J", "Q", "K", "A", "2"]

Output

false

Explanation

While these are high and low cards, a straight cannot 'wrap around' the Ace. The values are [11, 12, 13, 14, 2] or [11, 12, 13, 1, 2], neither of which is a consecutive 5-card sequence.

Constraints

  • cards.length == 5
  • cards[i] is one of {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
  • All elements in cards are unique.

Onsite

ArrayHash TableMathSimulationSortingString
Language
Code editor loads in the browser.

Output

Input

["2","3","4","5","6"]

Expected

true

Your output

Run to see your output.