commit 838ae094d71ebac895404c4c0dfe1d016b475555
parent 6a8a8d85c3f9104ece8ede5041a2fee2f0977bf6
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date: Mon, 22 May 2023 08:44:37 -0500
Completed letter combination of a phone number problem using a hashmap, recursion, backtracking, and dart.
Diffstat:
1 file changed, 43 insertions(+), 0 deletions(-)
diff --git a/letter-combinations-of-a-phone-number/letter-combinations-of-a-phone-number.dart b/letter-combinations-of-a-phone-number/letter-combinations-of-a-phone-number.dart
@@ -0,0 +1,43 @@
+//Given a list of numbers return all possible permutations that could result from the given inputs assuming that
+//each number can be any of the associated letters when looking at an old phone where you can access letters by
+//repeating numbers.
+//Time: 247ms Beats: 77.78%
+//Memory: 141MB Beats: 38.89%
+
+class Solution {
+ Map<int , List<String>> options = {};
+ int original_length = -1;
+ Solution(){
+ options = {
+ 2 : ["a" , "b" , "c"],
+ 3 : ["d" , "e" , "f"],
+ 4 : ["g" , "h" , "i"],
+ 5 : ["j" , "k" , "l"],
+ 6 : ["m" , "n" , "o"],
+ 7 : ["p" , "q" , "r" , "s"],
+ 8 : ["t" , "u" , "v"],
+ 9 : ["w" , "x" , "y" , "z"]
+ };
+ }
+ List<String> letterCombinations(String digits) {
+ if(original_length == -1){
+ original_length = digits.length;
+ if(digits.length == 0){
+ return [];
+ }
+ }
+ if(digits.length == 0){
+ return [""];
+ }
+ List<String> ret_list = [];
+ String new_digits = digits.substring(1);
+ List<String> vals = (options[int.parse(digits[0])] ?? []);
+ for(int x = 0 ; x < vals.length ; ++x){
+ List<String> returned_list = letterCombinations(new_digits);
+ for(int y = 0 ; y < returned_list.length ; ++y){
+ ret_list.add(vals[x] + returned_list[y]);
+ }
+ }
+ return ret_list;
+ }
+}