letter-combinations-of-a-phone-number.dart (1368B)
1 //Given a list of numbers return all possible permutations that could result from the given inputs assuming that 2 //each number can be any of the associated letters when looking at an old phone where you can access letters by 3 //repeating numbers. 4 //Time: 247ms Beats: 77.78% 5 //Memory: 141MB Beats: 38.89% 6 7 class Solution { 8 Map<int , List<String>> options = {}; 9 int original_length = -1; 10 Solution(){ 11 options = { 12 2 : ["a" , "b" , "c"], 13 3 : ["d" , "e" , "f"], 14 4 : ["g" , "h" , "i"], 15 5 : ["j" , "k" , "l"], 16 6 : ["m" , "n" , "o"], 17 7 : ["p" , "q" , "r" , "s"], 18 8 : ["t" , "u" , "v"], 19 9 : ["w" , "x" , "y" , "z"] 20 }; 21 } 22 List<String> letterCombinations(String digits) { 23 if(original_length == -1){ 24 original_length = digits.length; 25 if(digits.length == 0){ 26 return []; 27 } 28 } 29 if(digits.length == 0){ 30 return [""]; 31 } 32 List<String> ret_list = []; 33 String new_digits = digits.substring(1); 34 List<String> vals = (options[int.parse(digits[0])] ?? []); 35 for(int x = 0 ; x < vals.length ; ++x){ 36 List<String> returned_list = letterCombinations(new_digits); 37 for(int y = 0 ; y < returned_list.length ; ++y){ 38 ret_list.add(vals[x] + returned_list[y]); 39 } 40 } 41 return ret_list; 42 } 43 }