One of the problems I recently faced.

Input: A set of numbers from a mobile keypad

Output: Print all the combinations of characters for that given number.

For example if the input is 26. Then possible outputs are am, an, ao, bm, bn, bo, cm, cn, co

Solution:

There are many ways to solve this problem. One is using a tree. Another way is to simply do it with recursion. I solved this using a linear approach. If you convert the problem from the text domain to number domain - it will seem very simple.

For example - if the input is something like ['abc', 'def', 'pqrs'] (transformed from digits 237). Then the output would have a simple rule - the first character should come from the first element, second from the second and so on. Writing in an incremental number form for the indexes, you would get:

0 0 0

0 0 1

0 0 2

0 0 3

0 1 0

0 1 1

0 1 2

....

2 2 3

That's it - problem solved. Programatically expressed it here