Created
May 28, 2021 18:50
-
-
Save osahondev/bd56ea25ed06a282fd7b1be4fc855d73 to your computer and use it in GitHub Desktop.
Help Daniel play his game based on possible combination of number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const helpDaniel = ( nums ) => { | |
| if( nums == null || nums == undefined || nums.length == 0 ) return []; | |
| const output = []; | |
| const keys = [ | |
| "0", | |
| "1", | |
| "abc", // keypad 2 | |
| "def", // keypad 3 | |
| "ghi", // keypad 4 | |
| "jkl", // keypad 5 | |
| "mno", // keypad 6 | |
| "pqrs", // keypad 7 | |
| "tuv", // keypad 8 | |
| "wxyz" // keypad 9 | |
| ] | |
| computeCombination( nums,output,"",0,keys ); | |
| return output; | |
| } | |
| const computeCombination = ( nums,output,value,pos,keys ) =>{ | |
| if( pos == nums.length ){ | |
| output.push(value); | |
| return; | |
| } | |
| let alphabets = keys[ nums[pos] ]; | |
| for(let i=0; i<alphabets.length; i++){ | |
| computeCombination( nums,output,value+alphabets[i],pos+1,keys ); | |
| } | |
| } | |
| console.log( helpDaniel("2356") ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @enaiho, congratulations 🎉 your solution has been selected as one of the winning solutions in Week 8 of #AlgorithmFridays.
Your solution is clean, readable and passed the tests. I like the robust checks on line 4 and your use of recursion for solving this problem - that was neat!
Out of the many winning solutions, only 3 will be selected for the $20 prize in a raffle draw. The raffle draw will hold today, Friday June 4 at 3.00pm WAT (7.00 am PST)
If you are interested in being a part of the raffle draw, please send me a DM on Twitter @meekg33k so I can share the event invite with you.
NB: Only solutions of participants who indicated interest in the raffle draw will be considered.
Thanks once again for participating and see you later today for Week 9 of #AlgorithmFridays.