Implement the letterCombinationsPhoneCount method that counts letter combinations generated from phone digits.

The input is a string digits containing phone keypad digits.

Each digit maps to a group of letters, like 2 -> abc, 3 -> def, and so on. Your task is to count how many letter combinations can be formed by choosing one letter for each digit.

For example, digits 23 can form 3 × 3 = 9 combinations. If the input is empty, return 0.

Example 1

Input
digits (string) = 23
Returns (int) 9

Example 2

Input
digits (string) = 2
Returns (int) 3

Example 3

Input
digits (string) =
Returns (int) 0

Use the size of each digit's letter group.

Every digit contributes a number of choices. For digits 2, 3, 4, 5, 6, and 8, there are 3 choices. For digits 7 and 9, there are 4 choices.

Multiply the choices for all digits to get the total number of combinations.

Pseudocode:

function letterCombinationsPhoneCount(digits):
    if digits is empty:
        return 0
    map = {
        '2': 3, '3': 3, '4': 3, '5': 3,
        '6': 3, '7': 4, '8': 3, '9': 4
    }
    count = 1
    for each digit in digits:
        count = count * map[digit]
    return count
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.