Letter Combinations of a Phone Number

https://leetcode.com/problems/letter-combinations-of-a-phone-number/, here is the problem description (image):


This problem is commonly used in technical interviews. It is a good DFS problem, however the drawback that some might argue is that it is a one-solution problem: there are no multiple ways to solve it (other than recursive versus non-recursive, but the strategy is the same: DFS), and it is by design an exponential problem (although the limits are small). Notice that the problem asks for all the solutions, hence no DP or math shortcut can be applied.
The DFS is very straightforward:

  • Build a mapping between the phone digits and the strings (as given). I used an array of strings
  • The recursive call has three key parameters (the only ones changing):
    • The current index (which we'll use to index the digits)
    • The current combination
    • The solution (ref-based list of strings)
  • The DFS has two cases:
    • Base: when the index is greater than or equal to the digits length
    • Induction: going thru all the chars in the digits indexed by the current index
Code is down below, cheers, Marcelo.



    public class Solution
    {
        public IList<string> LetterCombinations(string digits)
        {
            if (String.IsNullOrEmpty(digits)) return new List<string>();
            string[] mapping = new string[10];

            mapping[0] = "";
            mapping[1] = "";
            mapping[2] = "abc";
            mapping[3] = "def";
            mapping[4] = "ghi";
            mapping[5] = "jkl";
            mapping[6] = "mno";
            mapping[7] = "pqrs";
            mapping[8] = "tuv";
            mapping[9] = "wxyz";

            List<string> result = new List<string>();
            LetterCombinationsInternal(0, "", digits, mapping, result);

            return result;
        }

        private void LetterCombinationsInternal(int currentPosition,
                                                string currentCombination,
                                                string digits,
                                                string[] mapping,
                                                List<string> result)
        {
            if (currentPosition >= digits.Length)
            {
                result.Add(currentCombination);
                return;
            }

            for (int i = 0; i < mapping[(int)(digits[currentPosition] - '0')].Length; i++)
            {
                LetterCombinationsInternal(currentPosition + 1,
                                           currentCombination + mapping[(int)(digits[currentPosition] - '0')][i].ToString(),
                                           digits,
                                           mapping,
                                           result);
            }
        }
    }

Comments

Popular posts from this blog

Changing the root of a binary tree

Prompt Engineering and LeetCode

ProjectEuler Problem 719 (some hints, but no spoilers)