LeetCode has a Medium coding Problem in Its’ Algorithm Section “Letter Combinations of a Phone Number”. Today We are going to solve this problem in Python. LeetCode Link of the Problem is HERE
Question
Given a string containing digits from 2-9
inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Examples
Input: digits = “23”
Output: [“ad”,”ae”,”af”,”bd”,”be”,”bf”,”cd”,”ce”,”cf”]
Input: digits = “”
Output: []
Input: digits = “2”
Output: [“a”,”b”,”c”]
Constraints:
0 <= digits.length <= 4
digits[i]
is a digit in the range['2', '9']
.
Solution to Letter Combinations of a Phone Number
def create(self,digits, index,m,ans,temp):
if index == len(digits):
return temp
for i in m[digits[index]]:
temp += i
if len(temp) == len(digits):
ans.append(temp)
else:
self.create(digits,index+1,m,ans,temp)
temp = temp[:-1]
return ans
Success
Runtime: 23 ms, faster than 56.19% of Python online submissions.
Memory Usage: 13.3 MB, less than 98.21% of Python online submissions.
READ MORE
Python-related posts Visit HERE
C++ related posts Visit HERE
Databases related posts Visit HERE
Data Structures related posts visit HERE
Algorithms related posts visit HERE
Data Science related posts visit HERE