class Solution { public boolean isValid(String s) { Deque<Character> deque = new LinkedList<>(); for(int i = 0; i < s.length(); i ++){ char ch = s.charAt(i); if(ch == '('){ deque.push(')'); } else if(ch == '{'){ deque.push('}'); } else if(ch == '['){ deque.push(']'); } else if(deque.isEmpty() || deque.peek() != ch){ return false; } else{ deque.pop(); } } return deque.isEmpty(); } }
class Solution { public String removeDuplicates(String s) { ArrayDeque<Character> deque = new ArrayDeque<>(); char ch; for(int i = 0; i < s.length(); i ++){ ch = s.charAt(i); if(deque.isEmpty() || deque.peek() != ch){ deque.push(ch); } else{ deque.pop(); } } String str = ""; while(!deque.isEmpty()){ str = deque.pop() + str; } return str; } }