Remove All Adjacent Duplicates In String | LeetCode | Java
Tanuja V
Posted on June 11, 2024
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();
}
}
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
💖 💪 🙅 🚩
Tanuja V
Posted on June 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.