Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package removingconsecutiveduplicates2;
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Stack;
- public class App {
- public static void main(String[] args) {
- String a = "aaabbbcccdddeeefff";
- System.out.println(removePair(a));
- }
- public static String removePair(String str) {
- Stack<Character> stack = new Stack<>();
- String output = "";
- String rev= "";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < str.length(); i++) {
- if (stack.isEmpty() || stack.peek() != str.charAt(i)) {
- stack.push(str.charAt(i));
- } else {
- stack.pop();
- }
- }
- while (!stack.isEmpty()) {
- output += stack.peek();
- stack.pop();
- }
- for(int i = output.length()-1;i>=0;i--){
- rev += output.charAt(i);
- }
- return rev;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement