Advertisement
rishu110067

Untitled

Feb 3rd, 2022
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1.    
  2.     public static List<string> get_words_from_phone_number(string phone_number) {
  3.         // Write your code here.
  4.         var phoneRef = new List<List<char>>(){
  5.             new List<char>(){' '},
  6.             new List<char>(){'a','b','c'},
  7.             new List<char>(){'d','e','f'},
  8.             new List<char>(){'g','h','i'},
  9.             new List<char>(){'j','k','l'},
  10.             new List<char>(){'m','n','o'},
  11.             new List<char>(){'p','q','r','s'},
  12.             new List<char>(){'t','u','v'},
  13.             new List<char>(){'w','x','y','z'}
  14.         };
  15.         var results = new List<string>();
  16.         var slate = new List<char>();
  17.         Helper(7,phoneRef,0,slate,results);
  18.        
  19.         return results;
  20.     }
  21.    
  22.     public static void Helper(int k, List<List<char>> phoneRef, int currentVal, List<char> slate, List<string> results){
  23.         if(k == 0){
  24.             results.Add(new string(slate.ToArray()).Trim());
  25.             return;
  26.         }
  27.         if(k < 0){
  28.             return;
  29.         }
  30.        
  31.         int i = currentVal;
  32.         for(int j = 0; j < phoneRef[i].Count;j++){
  33.             slate.Add(phoneRef[i][j]);
  34.             Helper(k-1, phoneRef, i+1, slate, results);
  35.             slate.RemoveAt(slate.Count-1);
  36.         }
  37.     }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement