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
Top comments (0)