17. Letter Combinations of a Phone Number
Approach 1: Generating all combinations one by one
This solution goes over each number in digits and appends the letters on that position to the end of all the existing strings in ans
For example, digits = '23'
, then ans = ['a','b','c'] # initialised from line 12
In the for loop, I come to '3'
and then for every element in ans
I append a letter from digit '3'
which means ans
becomes ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Time Complexity: O(N), where N is the total number of possible combinations that get returned
Space Complexity: O(N)
Last updated