C# simple solution


  • 0
    M
    public IList<string> LetterCombinations(string digits) {
        if(digits.Length == 0)
            return new List<string>();
            
        var result = new List<string>{""};
    
        foreach(char c in digits)
        {
            char c1 = (char)('a' + 3 * (c - '2'));
            int count = 3;
            
            if(c == '7' || c == '9')
                count = 4;
                
            if(c == '8')
                c1 = 't';
            else if(c == '9')
                c1 = 'w';
            
            var newResult = new List<string>();
            
            for(int i = 0; i < result.Count; i++)
            {
                for(char j = c1; j < c1 + count; j++)
                    newResult.Add(result[i] + j);
            }
            
            result = newResult;
        }
        
        return result;
    }

Log in to reply
 

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