This problem is so simple. Logically, we have to find the pair of adjacent letters and delete it. And then we have to traverse the array again and then find the pair of letters again and delete it. We have to repeat the process till no adjacent letters are found.
The above solution will work. But this is not the optimal solution.
The optimal solution would be traversing the array once. We can use the stack data-structure to solve this problem optimally.
Let’s see how
Initially, we start by traversing the array from start. And then if the stack is empty we push the element into the stack.
And for each element, we have to check if the top element of the stack is same as the current element. If it’s same then we pop the top element of the stack or else we push the current element into the stack.
We repeat this process till the end of the string.
That’s it. Simple right. :)