Given a number as a string where some of the digits are replaced by a ‘$’, the task is to generate all possible number by replacing the ‘$’ with any of the digits from the given string.
Input: str = “23$$”
Input: str = “$45”
- Find all the combinations of the string by replacing the character $ with any of the digits of the string, for this check if the current character is a digit if yes then store this character into an array pre then recursively find all of its combinations else if current character is a ‘$’ then replace it with the digits stored in the array and recursively find all the combinations.
- To find the all possible numbers initialize array set that stores all the possible numbers, to generate numbers take two nested loop outer loop is for input string and inner loop is for array set that stores all the possible combinations of the numbers. Initialize the boolean flag to check if character of the input string is already present in set or not if character of the input string is already present in set then set flag = false else if flag is true then move the current character of the input string to set and recursively find all the combinations of the input string and store it in the array set. Finally print each number from the array set
Below is the implementation of the above approach:
2322 2323 2332 2333