package leetcode;

import java.util.Stack;

/**
 * https://leetcode.com/problems/make-the-string-great/
 */
public class Problem1544 {
    public String makeGood(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            stack.push(s.charAt(i));
            if (stack.size() >= 2) {
                char a = stack.pop();
                char b = stack.pop();
                String aStr = "" + a;
                String bStr = "" + b;
                if ((!bStr.equals(bStr.toLowerCase()) || !aStr.equals(bStr.toUpperCase())) &&
                    (!bStr.equals(bStr.toUpperCase()) || !aStr.equals(bStr.toLowerCase()))) {
                    stack.push(b);
                    stack.push(a);
                }
            }
        }
        if (stack.size() >= 2) {
            char a = stack.pop();
            char b = stack.pop();
            String aStr = "" + a;
            String bStr = "" + b;
            if ((!bStr.equals(bStr.toLowerCase()) || !aStr.equals(bStr.toUpperCase())) &&
                (!bStr.equals(bStr.toUpperCase()) || !aStr.equals(bStr.toLowerCase()))) {
                stack.push(b);
                stack.push(a);
            }
        }
        String answer = "";
        for (char c : stack) {
            answer += c;
        }
        return answer;
    }
}

@@ -315,32 +315,22 @@
     
-if (stack.size() %3E= 2) %7B
+remove(stack);
 %0A   
@@ -330,32 +330,34 @@
 k);%0A        
+%7D%0A
         
 char a = sta
@@ -348,65 +348,57 @@
     
-char a = stack.pop();%0A                char b = stack.pop(
+if (stack.size() %3E= 2) %7B%0A            remove(stack
 );%0A 
@@ -396,32 +396,34 @@
 stack);%0A        
+%7D%0A
         String a
@@ -418,36 +418,34 @@
     String a
-St
+nswe
 r = %22%22
- + a
 ;%0A          
@@ -446,141 +446,64 @@
     
-        String bStr = %22%22 + b;%0A                if ((!bStr.equals(bStr.toLowerCase()) %7C%7C !aStr.equals(bStr.toUpperCase())) &&%0A 
+for (char c : stack) %7B%0A            answer += c;%0A
         
     
@@ -494,35 +494,34 @@
  c;%0A        
-   
+%7D%0A
         
 (!bStr.equal
@@ -512,223 +512,83 @@
     
-(!bStr.equals(bStr.toUpperCase()) %7C%7C !aStr.equals(bStr.toLowerCase()))) %7B%0A                    stack.push(b);%0A                    stack.push(a);%0A                %7D%0A            %7D%0A        %7D%0A        if (stack.size() %3E= 2
+return answer;%0A    %7D%0A%0A    private static void remove(Stack%3CCharacter%3E stack
 ) %7B%0A
@@ -591,28 +591,24 @@
 ) %7B%0A        
-    
 char a = sta
@@ -621,28 +621,24 @@
 ();%0A        
- 
-   
 char b = sta
@@ -651,28 +651,24 @@
 ();%0A        
- 
-   
 String aStr 
@@ -681,28 +681,24 @@
  a;%0A        
-  
-  
 String bStr 
@@ -711,28 +711,24 @@
  b;%0A        
-  
-  
 if ((!bStr.e
@@ -805,20 +805,16 @@
         
-    
 (!bStr.e
@@ -875,28 +875,24 @@
 Case()))) %7B%0A
-    
             
@@ -910,36 +910,32 @@
 b);%0A            
-    
 stack.push(a);%0A 
@@ -945,140 +945,9 @@
     
-    %7D%0A        %7D%0A        String answer = %22%22;%0A        for (char c : stack) %7B%0A            answer += c;%0A        %7D%0A        return answer;
+%7D
 %0A   

Update problem 1544