Easy and clean recursive solution in c#


  • 0
    C
            String[] mapping = new String[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    
            public IList<string> LetterCombinations1(string digits)
            {
                List<string> result = new List<string>();
                InnertLetterCombinations("", digits, result);
                return result;
            }
    
            private void InnertLetterCombinations(string tmp, string digits, IList<string> result)
            {
                if (digits.Length == 0)
                {
                    result.Add(tmp);
                    return;
                }
                for (int i = 0; i < mapping[digits[0] - '0'].Length; i++)
                {
                    InnertLetterCombinations(tmp + mapping[digits[0] - '0'][i], digits.Substring(1), result);
                }
            }
    

Log in to reply
 

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