C# solution


  • 1
    D
    public class Solution {
        static Dictionary<char, List<string>> mapping = new Dictionary<char, List<string>>(){
            {'0', new List<string>()}, {'1', new List<string>()}, {'2', new List<string>(){"a","b","c"}}, 
            {'3', new List<string>(){"d","e","f"}}, {'4', new List<string>(){"g","h","i"}}, {'5', new List<string>(){"j","k","l"}},
            {'6', new List<string>(){"m","n","o"}}, {'7', new List<string>(){"p","q","r","s"}}, {'8', new List<string>(){"t","u","v"}},
            {'9', new List<string>(){"w","x","y","z"}}};
            
        public IList<string> LetterCombinations(string digits) {
            List<string> res = new List<string>();
            GetAllCombinations(digits, res, 0, string.Empty);
            return res;
        }
        
        public void GetAllCombinations(string digits, List<string> res, int curr, string current)
        {
            if(curr >= digits.Length)
            {
                if(current.Length > 0)
                    res.Add(current);
                return;    
            }
            
            foreach(var s in mapping[digits[curr]])
                GetAllCombinations(digits, res, curr + 1, current+s);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.