# Use backtrack function to generate all possible combinations def backtrack(index, path, digits, letters, combinations): if len(path) == len(digits): combinations.append(''.join(path)) return possible_letters = letters[digits[index]] if possible_letters: for letter in possible_letters: path.append(letter) backtrack(index + 1, path, digits, letters, combinations) path.pop() def letter_combinations(digits): combinations = [] if len(digits) == 0: return [] digits_mapping = { "1": [""], "2": ["a", "b", "c"], "3": ["d", "e", "f"], "4": ["g", "h", "i"], "5": ["j", "k", "l"], "6": ["m", "n", "o"], "7": ["p", "q", "r", "s"], "8": ["t", "u", "v"], "9": ["w", "x", "y", "z"]} backtrack(0, [], digits, digits_mapping, combinations) return combinations # driver code def main(): digits_array = ["23", "73", "426", "78", "925", "2345"] counter = 1 for digits in digits_array: print(counter, ".\t All letter combinations for '", digits, "': ", letter_combinations(digits), sep="") counter += 1 print("-" * 100) if __name__ == "__main__": main()