


A valid IP address consists of exactly four integers, each integer is between 0 and 255 separated by single dots, and cannot have leading zeros except in the case of zero itself.
The following are valid IP addresses:
0.1.24.255
18.5.244.1
Following are invalid IP addresses:
0.01.24.255 (as 01 contains one leading zero).
18.312.244.1 (as 312 not lies between 0 and 255).
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run.
Then the 'T' test cases follow.
The first line of each test case contains a string 'S'.
For each test case, print a single line containing all possible valid IP addresses that can be obtained from 'S' in lexicographical order in a separate line.
Each IP address of a string 'S' is written within quotes("") and separated by comma(,) and space, and all IP addresses of the given string 'S' is written inside square brackets[].
1 <= T <= 1000
1 <= |S| <= 15
Where |'S'| denotes the length of string 'S' and 'S' contains only digits from 0 to 9.
Time Limit: 1 sec
You do not need to print anything, it has already been taken care of. Just implement the given function.
The idea is to divide the given string into 4 parts using 3 dots, where each part corresponds to a number. Then we will add the current IP address to answer if it satisfies the following conditions:
So we will run 4 nested loops from 1 to 3.
The idea is here to use backtracking to generate all possible IP addresses.The key observation here is we can select either 1 or 2 or 3 digits at a time and put it into one segment. So In each step, we will choose 1 or 2 or 3 digits and move to the next segment. Before moving to the next segment we will check if the current segment is valid or not. If it’s invalid then there is no need to visit this path.
Backtracking Approach consist of the following things :
void backtracking(s, answer,segments, curIndex, segmentIndex):