Letter Combinations Count
Medium backtracking · strings
Description
Constraints
0 <= length <= 4; digits 2-9
Examples
Input: digits = "23"
Output: 9
Hints (1)
- Multiply the number of letters for each digit.
Solutions
Product of letter counts. · Time O(n) · Space O(1)
func letterCombinationsCount(digits string) int {
if digits == "" {
return 0
}
m := map[byte]int{'2': 3, '3': 3, '4': 3, '5': 3, '6': 3, '7': 4, '8': 3, '9': 4}
total := 1
for i := 0; i < len(digits); i++ {
total *= m[digits[i]]
}
return total
}
Product of letter counts. · Time O(n) · Space O(1)
function letterCombinationsCount(digits) {
if (digits === '') return 0;
const map = { '2': 3, '3': 3, '4': 3, '5': 3, '6': 3, '7': 4, '8': 3, '9': 4 };
let total = 1;
for (const d of digits) total *= map[d];
return total;
}
Product of letter counts. · Time O(n) · Space O(1)
function letterCombinationsCount($digits) {
if ($digits === '') { return 0; }
$map = ['2'=>3,'3'=>3,'4'=>3,'5'=>3,'6'=>3,'7'=>4,'8'=>3,'9'=>4];
$total = 1;
for ($i = 0; $i < strlen($digits); $i++) { $total *= $map[$digits[$i]]; }
return $total;
}
Product of letter counts. · Time O(n) · Space O(1)
def letterCombinationsCount(digits):
if digits == "":
return 0
m = {"2":3,"3":3,"4":3,"5":3,"6":3,"7":4,"8":3,"9":4}
total = 1
for d in digits:
total *= m[d]
return total
Product of letter counts. · Time O(n) · Space O(1)
function letterCombinationsCount(digits: string): number {
if (digits === '') return 0;
const map: Record<string, number> = { '2': 3, '3': 3, '4': 3, '5': 3, '6': 3, '7': 4, '8': 3, '9': 4 };
let total = 1;
for (const d of digits) total *= map[d];
return total;
}
Create a free account to submit solutions, track progress and save problems.
Create account