LeetCode 17. Letter Combinations of a Phone Number

Description

https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example:

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Explanation

First, build a map which stores relationships between numbers and the letters they represent.

Then, use depth-first seach approach to find all letter combinations.

Video Tutorial

Java Solution

One thought

  1. Hello, how can I reach you privately?
    my email is ljustincoder@gmail.com. I need help in understanding some of your solutions.
    Secondly, regarding the letter combinations of a phone number. I understand what your code does but my recursion skill is weak and need clarity on one condition. So, say digit is “23”. the first time the helper is called. sb is “”. but what is the index of “”? how did the computer know that when you do :
    lettersMap.get(digits.charAt(sb.length())), it is to get the the index 2? is “” equated to 0?

    then, it gets the first character in the key 2 and appends it to the StringBuilder making the sb.length=1. Ok. I get it, it picks the map with key index 1 which is 3 and begins to add its’ characters. and each time the sb.length==digit.length, it adds the StringBuilder to the result list. and then removes that character. it continues until even the character a from the phone number 2 is removed, defaulting the StringBuilder sb to “” again. at this stage, the result is “ad, ae, af”.
    Now, this is where I don’t understand it. Because instead of picking character a again, it picks b. how was the programme able to know that the first character in phone number key 2, which is ‘a’ should be overlooked?

    This is what I don’t get.

    Lastly, have you considered setting up a patron page where people like me can donate something, no matter how small, to encourage you? I am glued to your youtube videos and seriously testing the codes to make sense of your thought processes. Please, don’t stop. You are the only one explaining it simplistically. Is there a way to also send you Leetcode questions I find challenging so you can solve and explain to the community?

Leave a Reply

Your email address will not be published. Required fields are marked *