C#: concise, build-up approach, using LINQ


  • 0
    G
    public class Solution {
        public IList<string> LetterCombinations(string digits) {
            if (string.IsNullOrEmpty(digits)) return new List<string>();
            
            var combinations = new List<string> {""};
            string[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    
            foreach (int digit in digits.Select(d => d - '0')) {
                if (digit == 0 || digit == 1) return new List<string>();
                combinations = (from letter in map[digit] from combination in combinations select combination + letter).ToList();
            }
    
            return combinations;
        }
    }
    

Log in to reply
 

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