DevPath

← All problems

Letter Combinations Count

Medium backtracking · strings

Description

Given a phone number string containing digits 2-9, return how many letter combinations it could produce.

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
}

Create a free account to submit solutions, track progress and save problems.

Create account