I was faced with a question recently in C. We have a phone's numpad with following layout:
1[abc] 2[def] 3[ghi]
4[jkl] 5[mno] 6[pqr]
7[st] 8[uv] 9[wx]
0[yz]
How to come up with an API which gives all possible combinations of characters belonging to each number for a given numeral input.
For e.g. input = 1234
Then the API should print all possible combinations of characters-
adgj bdgj cdgj aegj begj cegj.. and so on.
Is there a simple way to do it? Apart from hardcoded nested for loops.
I was given a hint as recursion but couldn't figure a way out of it.
A way to find the combinations that you are looking for could be bitwise logic, with a binary number and an integer. The binary number would be as long as the string, with 0's and 1's acting as on and off switches for what is included and excluded in the string. The thing here is that we use base 3 or 4 depending on the number "pressed", and
If base four, then some if statements have to be applied to move the ones along that are actually base three.