Haibara hates that Conan is always grasped in murder mysteries. So this time she gives Conan a simple problem to solve in which he is given a string 'S' consisting of lowercase and uppercase characters.
She tells him that a "Magical String" is a string that does not have an adjacent pair of the same characters with one character being lowercase and the other being uppercase. Formally, 'S' is magical if there exits no 'i' such that 'S[i]' is a lowercase character and 'S[ i + 1]' is the same character but in uppercase or vice-versa.
Now, Conan has to make the string 'S' magical by removing the minimum number of characters and return Hibara the magical string hence made.
Your task is to help Conan to obtain the magical string.
Note :
An empty string is also magical.
Input Format :
The first line contains a single integer ‘T’ representing the number of test cases.
The next ‘T’ lines contain a string ‘S’ given by Haibara.
Output Format :
For each test case, print the magical string ‘S’ by removing the minimum number of characters.
Output for every test case will be printed in a separate line.
Note :
You don’t need to print anything; It has already been taken care of.
1 <= T <= 50
1 <= |S| <= 100
Where ‘T’ is the number of test cases and |S| is the length of the string ‘S.’
Time limit: 1 sec
2
sweeEet
codeE
sweet
cod
In the first test case, either you choose i = 3 or i = 4, both will have the same result “sweet”.
In the second test case, there is only one option i.e i = 3. Hence the result will be “cod”
2
abBAcC
ninja
ninja
In the first test case, first of all, we will choose 'i' =1, which will lead us to “aAcC”. Now we have to choose 'i' = 0, to remove “aA” which will lead us to “cC”. Now we have to remove 'i' = 0 again to make the string magical.
“abBAcC” -> “aAcC” -> “cC” -> “”
In the second test, the given string is already magical. So the result is the same as the input i.e “ninja”.
The difference between an upper case and a lower case alphabet is 32.
We will push the first char of the string in a stack and check if the topmost element in the stack and the next element of the string has a difference of 32. Then we will pop the last element from the stack and move to the next element of the string ‘S’. Otherwise, if the difference is not 32 which means that they are not same characters so we will push that element of the string in the stack.
The steps are as follows:
O(|S|), where |S| is the size of the string.
We iterate over the whole string to check if there are any two characters having difference 32. Hence the overall time complexity is O(|S|).
O(|S|), where |S| is the size of the string.
We are using a stack to store the answer to the string. Hence the space complexity is O(|S|).