Problem of the day
You are given a string 'S'. Your task is to find an array of all possible strings in any order that can be formed by transforming every letter individually to the lowercase or the uppercase.
Note:
1. You can print the array in any order.
2. The string 'S' only contains English alphabets and digits.
The first line contains an integer T, which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first and only line of each test case contains a string 'S', as described in the problem statement.
Output Format:
For each test case, print a single line containing space-separated strings denoting all possible strings by transforming, as described in the problem statement.
The output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= |S| <= 12
Where |S| denotes the length of string 'S'.
Time Limit: 1 sec.
1
a1b
A1B A1b a1B a1b
These are the four strings that we can get after transforming every letter individually to be lowercase or uppercase.
1
0
0
There is no alphabet in the string, so we get the output same as the input string.
Think of a recursive solution.
The idea here is to use the recursion. For each index i, where 0 <= i < |S|, if S[i] is a digit, then there is only one option available, which is to call the function again with i + 1, and if S[i] is a letter, then there are two options available. The first one is to change S[i] to the lowercase (if already is in the lowercase, then doesn’t matter) and call the function again with i + 1. The second one is to change S[i] to the uppercase (if already is in the uppercase, then doesn’t matter) and call the function again with i + 1.
When i becomes equal to the length of the string S, then just add the string S to the answer array and which is also the base condition for the recursion function. So, we can say that for each index, if it a letter, then two options are available, which may go up to 2 ^ N (in the worst case), where N is the length of the string.
Steps:
void stringPer(string S, int ind, int ans[]) :
O(2 ^ N), where N is the size of the string.
We are using the recursion. In each function call, in the worst case (if all characters in the string are letters), we have two options that are to convert into lowercase and convert into uppercase. As the size of the string is N, the time complexity will go up to O(2 ^ N).
O(N), where N is the size of the string.
We are using recursion, a recursion stack is formed as we are looking for all possible strings that can be formed by the transformation, In the worst case, the depth of the recursion tree may go up to N. Hence, the space complexity is O(N).