Letter Combinations of a Phone Number

Recursion Hard Hard

Given a string consisting of digits from 2 to 9 (inclusive). Return all possible letter combinations that the number can represent.


Mapping of digits to letters is given in first example.

Examples:

Input : digits = "34"

Output : [ "dg", "dh", "di", "eg", "eh", "ei", "fg", "fh", "fi" ]

Explanation : The 3 is mapped with "def" and 4 is mapped with "ghi".

So all possible combination by replacing the digits with characters are shown in output.

Input : digits = "3"

Output : [ "d", "e", "f" ]

Explanation : The 3 is mapped with "def".

Input : digits = "8"

Constraints

  • 0 <= digits.length <= 4
  • digts[i] contains digitd from [2,9].

Hints

  • Use recursion to explore all combinations. For each digit, iterate over its mapped letters. Append each letter to the current combination and proceed to the next digit.
  • Start with an empty combination in a queue. For each digit, extend all existing combinations in the queue by appending each possible letter. Continue until all digits have been processed.

Company Tags

Stripe HCL Technologies IBM Goldman Sachs Boston Consulting Group Broadcom Dropbox Epic Systems Cerner Flipkart Ernst & Young Epic Games Chewy HashiCorp Qualcomm Square Morgan Stanley MongoDB Micron Technology OYO Rooms Intel Mastercard Shopify Nutanix Seagate Technology Google Microsoft Amazon Meta Apple Netflix Adobe