Given a string of some symbols, write a program to transform a string by repeatedly eliminating two same symbols occurring together.
For example if the input string is ‘RGGB’ it will be reduced to ‘RB’ because ‘GG’ can be eliminated.
Here are some more examples of input – output
Input | Output
GBRRBR | GR
BGRGRR | BGRG
GGBB | <empty>
RGBGBR | RGBGBR
Here is how we can solve this problem. Add each character into a temporary array. As you insert the character, check for previously inserted character. If it is same, erase that character otherwise add it to the temporary array. At the end, the temporary array has the transformed string.
C++ implementation of the above approach is given below.