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 digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
- Input: digits = "23"
- Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
- Input: digits = ""
- Output: []
- Input: digits = "2"
- Output: ["a","b","c"]
- 0 <= digits.length <= 4
- digits[i] is a digit in the range ['2', '9'].
var m = map[uint8][]string{
'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"},
}
func letterCombinations(digits string) []string {
res := make([]string, 0)
if digits == "" {
return res
}
letters := m[digits[0]]
lastCombinations := letterCombinations(digits[1:])
for i := 0; i < len(letters); i++ {
if len(lastCombinations) == 0 {
res = append(res, letters[i])
} else {
for j := 0; j < len(lastCombinations); j++ {
res = append(res, letters[i]+lastCombinations[j])
}
}
}
return res
}
-
The function
letterCombinations
is called recursively, and each recursive call reduces the length of the input stringdigits
by 1. This means that the recursion depth is equal to the length of the input stringdigits
, which we can denote as n. -
For each level of the recursion, the code iterates through the possible letters of the current digit (using the
for i
loop) and combines them with the combinations returned by the recursive call (using thefor j
loop). In the worst case, a digit maps to 4 characters (e.g., '7' or '9'), so the maximum number of iterations for both loops is 4. -
The cost of the string concatenation operation (
letters[i] + lastCombinations[j]
) can be assumed to be constant time, as the length of the concatenated strings is equal to the recursion depth, which is at most n.
Considering the points above, the time complexity of the algorithm can be represented as
Runtime: 0 ms, faster than 100.00% of Go online submissions for Letter Combinations of a Phone Number.
-
The space complexity is affected by the depth of the recursion, the number of function calls, and the size of the result array res.
-
The depth of the recursion is n, as mentioned earlier.
-
In each level of the recursion, the size of the result array increases. In the worst case, each digit maps to 4 characters, and since we're forming combinations of all characters, the size of the result array can grow up to 4^n.
-
Additionally, each string in the result array has a length at most n, which also contributes to the space complexity.
Considering the points above, the space complexity of the function can be represented as
Memory Usage: 2 MB, less than 78.41% of Go online submissions for Letter Combinations of a Phone Number.