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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs