DEV Community

Tanuja V
Tanuja V

Posted on • Updated on

Remove All Adjacent Duplicates In String | LeetCode | Java

class Solution {
    public String removeDuplicates(String s) {

        Stack<Character> stack = new Stack<>();

        char arr[] = s.toCharArray();

        for(char ch : arr){
           if(!stack.isEmpty()){
                if(stack.peek()==ch)
                    stack.pop();
               else
                stack.push(ch);
           }

            else
                stack.push(ch);
        }

        String res = "";

        StringBuilder sb = new StringBuilder();

        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }

        return sb.reverse().toString();
    }
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading :)
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀

If you enjoy my content, support me by following me on my other socials:
https://linktr.ee/tanujav7

Top comments (0)