Skip to content
BASA DSA

Letter Combinations of a Phone Number

MediumBacktracking

Return every letter string formed by choosing one telephone-keypad letter per digit in order. Digits 2..9 map to abc, def, ghi, jkl, mno, pqrs, tuv, wxyz.

Example 1

Input
digits = "24"
Output
["ag","ah","ai","bg","bh","bi","cg","ch","ci"]

Explanation

Choose from abc for 2 and ghi for 4, producing nine ordered strings.

Example 2

Input
digits = ""
Output
[]

Explanation

Empty digits produce no letter strings under this contract.

Example 3

Input
digits = "7"
Output
["p","q","r","s"]

Explanation

Digit 7 has four letters: p,q,r,s.

Constraints

  • 0 <= number of digits <= 4
  • Digits are 2..9
  • Empty input returns an empty result

Function contractletterCombinationsArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…