Given n lists of characters and a number whose digits lie between 1 and n, print all possible combinations by replacing its digits with the characters of the corresponding list. If any digit of the number gets repeated, it should be replaced by the same character considered in its previous occurrence.

For example,

Input:
 
list[1] —> { ‘A’, ‘B’, ‘C’, ‘D’ }
list[2] —> { ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’ }
list[3] —> { ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’ }
list[4] —> { ‘R’, ‘S’, ‘T’ }
list[5] —> { ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’ }
 
key = 131
 
Output:
 
ALA AMA ANA AOA APA AQA BLB BMB BNB BOB BPB BQB CLC CMC CNC COC CPC CQC DLD DMD DND DOD DPD DQD

Practice this problem

We can use recursion to solve this problem. The idea is to consider every digit of the given key one by one, and for every digit, there are two possibilities:

  • If the digit is seen for the first time, replace it with each character in the corresponding list and recur for the next digit.
  • If the digit is seen before, replace it with the same character used in the previous occurrence.

To store the mapping of digits to characters of the list, use a map. If every digit of the key is processed, print the modified key. Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
BOB DMD BNB AMA BPB BLB DND AOA DPD CQC BMB ANA ALA DOD DQD AQA BQB CLC CMC CNC COC DLD APA CPC

The time complexity of the above solution is exponential and requires additional space for the recursion (call stack).