package com.fishercoder.solutions;

/**
 * 640. Solve the Equation
 *
 * Solve a given equation and return the value of x in the form of string "x=#value".
 * The equation contains only '+', '-' operation, the variable x and its coefficient.

 If there is no solution for the equation, return "No solution".

 If there are infinite solutions for the equation, return "Infinite solutions".

 If there is exactly one solution for the equation, we ensure that the value of x is an integer.

 Example 1:
 Input: "x+5-3+x=6+x-2"
 Output: "x=2"

 Example 2:
 Input: "x=x"
 Output: "Infinite solutions"

 Example 3:
 Input: "2x=x"
 Output: "x=0"

 Example 4:
 Input: "2x+3x-6x=x+2"
 Output: "x=-1"

 Example 5:
 Input: "x=x+2"
 Output: "No solution"

 */
public class _640 {
    /**
     * Reference: https://discuss.leetcode.com/topic/95203/concise-java-solution/7
     */
    public String solveEquation(String equation) {
        String[] parts = equation.split("=");
        int[] left = evaluate(parts[0]);
        int[] right = evaluate(parts[1]);
        if (left[0] == right[0] && left[1] == right[1]) {
            return "Infinite solutions";
        } else if (left[0] == right[0]) {
            return "No solution";
        }
        return "x=" + (right[1] - left[1]) / (left[0] - right[0]);
    }

    private int[] evaluate(String part) {
        int[] result = new int[2];//result[0] is the coefficient for x, result[1] is the coefficient for constants
        String[] tokens = part.split("(?=[+-])");  // ()for match group; ?= for match and include in res; [+-] means + or -;
        for (String token : tokens) {
            if (token.equals("+x") || token.equals("x")) {
                result[0]++;
            } else if (token.equals("-x")) {
                result[0]--;
            } else if (token.contains("x")) {
                result[0] += Integer.parseInt(token.substring(0, token.length() - 1));
            } else {
                result[1] += Integer.parseInt(token);
            }
        }
        return result;
    }
}

@@ -761,16 +761,56 @@
  _640 %7B%0A
+    public static class Solution1 %7B%0A    
     /**%0A
@@ -814,16 +814,20 @@
 **%0A     
+ 
+   
 * Refere
@@ -905,15 +905,23 @@
     
-*/%0A
+    */%0A   
     
+ 
 publ
@@ -959,24 +959,28 @@
 equation) %7B%0A
+    
         Stri
@@ -1017,24 +1017,28 @@
 %22);%0A        
+    
 int%5B%5D left =
@@ -1062,24 +1062,28 @@
 %5D);%0A        
+ 
+   
 int%5B%5D right 
@@ -1100,24 +1100,28 @@
 (parts%5B1%5D);%0A
+    
         if (
@@ -1170,32 +1170,36 @@
 ) %7B%0A            
+   
+ 
 return %22Infinite
@@ -1211,16 +1211,20 @@
 tions%22;%0A
+    
         
@@ -1261,32 +1261,36 @@
 ) %7B%0A            
+    
 return %22No solut
@@ -1295,34 +1295,42 @@
 ution%22;%0A        
+ 
-%7D%0A
+   %7D%0A    
         return %22
@@ -1384,15 +1384,23 @@
 %5D);%0A
+    
     %7D%0A%0A
+    
     
@@ -1433,24 +1433,28 @@
 ing part) %7B%0A
+    
         int%5B
@@ -1556,32 +1556,36 @@
 nstants%0A        
+  
+  
 String%5B%5D tokens 
@@ -1693,16 +1693,20 @@
         
+    
 for (Str
@@ -1723,24 +1723,28 @@
 : tokens) %7B%0A
+    
             
@@ -1798,32 +1798,36 @@
                 
+    
 result%5B0%5D++;%0A   
@@ -1823,16 +1823,20 @@
 t%5B0%5D++;%0A
+    
         
@@ -1880,32 +1880,36 @@
                 
+  
+  
 result%5B0%5D--;%0A   
@@ -1905,16 +1905,20 @@
 t%5B0%5D--;%0A
+    
         
@@ -1947,32 +1947,36 @@
 ontains(%22x%22)) %7B%0A
+    
                 
@@ -2054,24 +2054,28 @@
             
+    
 %7D else %7B%0A   
@@ -2071,16 +2071,20 @@
  else %7B%0A
+    
         
@@ -2141,26 +2141,33 @@
         
+  
-%7D%0A
+  %7D%0A   
         
 %7D%0A      
@@ -2150,34 +2150,39 @@
    %7D%0A           
+ 
 %7D%0A
+    
         return r
@@ -2188,16 +2188,26 @@
 result;%0A
+        %7D%0A
     %7D%0A%7D%0A

refactor 640