Array ( [0] => [1] => questions [2] => Strings [3] => Combination-of-strings-used-to-dial-a-number )
Given a number N. You have to print the all the combination of strings that can be used to dial the given number.
First line contains the number N.
Print all the combinations.
2
A B C
23
AD AE AF BD BE BF CD CE CF
24
AG AH AI BG BH BI CG CH CI
Login to see Discussion
step-i) Create a HashMap which keeps the numbers as keys and respective characters as values
step-ii) create a string builder str
step-iii) call the method print_answer passing N,i which is 0,hashmap and str as parameters
step-iv) if i equal to length of string print string and return
step-v) initialize another string s with value of hashmap whose key is character at index i of given string
step-vi) initialize j = 0
step-vii)append character at j in s string to the string builder
step-viii)call the print function passing N,i+1, hashmap and str as parameters
step-ix)from string builder delete chatacter present at last and increment j by one value
step-x) repeat steps vii, viii and ix till j is less than length of string s
Time Complexity: O(n)
Space Complexity: O(n)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution