Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<string> get_words_from_phone_number(string phone_number) {
- // Write your code here.
- var phoneRef = new List<List<char>>(){
- new List<char>(){' '},
- new List<char>(){'a','b','c'},
- new List<char>(){'d','e','f'},
- new List<char>(){'g','h','i'},
- new List<char>(){'j','k','l'},
- new List<char>(){'m','n','o'},
- new List<char>(){'p','q','r','s'},
- new List<char>(){'t','u','v'},
- new List<char>(){'w','x','y','z'}
- };
- var results = new List<string>();
- var slate = new List<char>();
- Helper(phone_number.Length,phoneRef,0,slate,results,phone_number);
- // result can be empty for strings having only 0 and 1
- if(results.Count == 0){
- results.Add("-1");
- }
- return results;
- }
- public static void Helper(int k, List<List<char>> phoneRef, int currentVal, List<char> slate, List<string> results, string phone_number){
- if(k == 0){
- // Don't append slate to result if slate is empty
- if(slate.Count != 0){
- results.Add(new string(slate.ToArray()).Trim());
- }
- return;
- }
- if(k < 0){
- return;
- }
- // if the number at currentVal is 0 or 1, then don't add anything to slate and move ahead
- if(phone_number[currentVal] == '0' || phone_number[currentVal] == '1'){
- Helper(k-1, phoneRef, currentVal+1, slate, results, phone_number);
- return;
- }
- int i = phone_number[currentVal] - '1';
- for(int j = 0; j < phoneRef[i].Count;j++){
- slate.Add(phoneRef[i][j]);
- Helper(k-1, phoneRef, currentVal+1, slate, results, phone_number);
- slate.RemoveAt(slate.Count-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement