MediumBacktrackingRecursionString

1 approach, code in every language

Problem description

Given a string of digits between 2 and 9, imagine the classic telephone keypad where each of those digits maps to a small group of letters. Produce every possible string of letters that could be typed by pressing the given digits in order, choosing one letter per digit.

Return all such combinations in any order. If the input string is empty, there are no letters to choose and the answer is an empty list.

Example 1

Input: digits = "23"

Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

Digit two offers a, b, c and digit three offers d, e, f, so every pairing is produced.

Example 2

Input: digits = ""

Output: []

No digits means there is nothing to combine.

Constraints

  • 0 <= digits.length <= 4
  • Each character in digits is a digit from 2 through 9
  • No two returned combinations are identical

After the pattern

Now solve one you haven't seen.

Keypad Code Spellings is our own problem on the technique behind Letter Combinations of a Phone Number: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free