daily pastebin goal
59%
SHARE
TWEET

PhoneNumberIterative

a guest Feb 14th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public IList<string> LetterCombinations(string digits)
  2.     {
  3.         var result = new List<string>();
  4.  
  5.             var numberToLetters = new[] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
  6.  
  7.             if (string.IsNullOrEmpty(digits))
  8.                 return result;
  9.  
  10.             if (digits.Any(x => !char.IsDigit(x)))
  11.                 return result;
  12.  
  13.             var queue = new Queue<string>();
  14.             queue.Enqueue("");
  15.  
  16.             for (var i = 0; i < digits.Length; i++)
  17.             {
  18.                 while (queue.Peek().Length == i)
  19.                 {
  20.                     var temp = queue.Dequeue();
  21.                     foreach (var letter in numberToLetters[digits[i] - 48])
  22.                     {
  23.                         queue.Enqueue(temp + letter);
  24.                     }
  25.                 }
  26.             }
  27.             return queue.ToList();
  28.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top