Super Reduced String

  • + 0 comments

    Solution using Stack in java:

     public static String superReducedString(String s) {
            String result = "";
            Stack<Character> stack = new Stack<>();
            int n = s.length();
            for(int i = 0; i < n; i++){
                char c = s.charAt(i);
                if(stack.empty() || c != stack.peek())
                    stack.push(c);
                else
                    stack.pop();
                }
            if(stack.empty())
                result = "Empty String";
            
            else
              for(char c: stack)
                  result += c;
            
            return result;        
            }