/17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number

Medium
Hash Table65.6% acceptance

Given a string input_digits consisting of digits from 2 to 9, return a list of all possible strings that can be formed by mapping each digit to its corresponding set of letters (as per the standard telephone keypad mapping). The order of the output list does not matter.

Example 1

Input: "34"

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

Explanation: Digit 3 maps to [d,e,f], digit 4 maps to [g,h,i].

Example 2

Input: "7"

Output: [p, q, r, s]

Explanation: Digit 7 maps to [p,q,r,s].

Example 3

Input: "89"

Output: [ta, tb, tc, ua, ub, uc, va, vb, vc]

Explanation: Digit 8 maps to [t,u,v], digit 9 maps to [a,b,c].

Constraints

  • 1 <= len(input_digits) <= 4
  • Each character in input_digits is in {2,3,4,5,6,7,8,9}
Python (current runtime)

Case 1

Input: "56"

Expected: ['jm', 'jn', 'jo', 'km', 'kn', 'ko', 'lm', 'ln', 'lo']

Case 2

Input: "222"

Expected: ['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc', 'baa', 'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa', 'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']

Case 3

Input: "5"

Expected: ['j', 'k', 'l']

Case 4

Input: "79"

Expected: ['pa', 'pb', 'pc', 'qa', 'qb', 'qc', 'ra', 'rb', 'rc', 'sa', 'sb', 'sc']