My C# solution - would love to hear your improvements


  • 0
    N
        public IList<string> LetterCombinations(string digits) 
        {
            if(digits.Length ==0)
            return new List<string>();
            
            string[] letters = new string[]{"","","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            IList<string> result = new List<string>(){""};
            
            foreach (char c in digits)
            {
                List<string> temp = new List<string>();
                foreach(char k in letters[c-'0'])
                {
                    foreach(string s in result)
                    {
                        temp.Add(s+k);
                    }
                }
                result = temp;
            }
            return result;
        }
    

Log in to reply
 

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