DEV Community

Tanuja V
Tanuja V

Posted on • Edited on

3

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay