package leetcode;

/**
 * https://leetcode.com/problems/solve-the-equation/
 */
public class Problem640 {
    public String solveEquation(String equation) {
        // TODO
        return "";
    }

    public static void main(String[] args) {
        Problem640 prob = new Problem640();
        System.out.println(prob.solveEquation("x+5-3+x=6+x-2")); // x=2
        System.out.println(prob.solveEquation("x=x")); // Infinite solutions
        System.out.println(prob.solveEquation("2x=x")); // x=0
        System.out.println(prob.solveEquation("2x+3x-6x=x+2")); // x=-1
        System.out.println(prob.solveEquation("x=x+2")); // No solution
    }
}

@@ -162,414 +162,1352 @@
     
-// TODO%0A        return %22%22;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem640 prob = new Problem640(
+String%5B%5D e = equation.split(%22=%22);%0A        String lhs = e%5B0%5D;%0A        VarConst lhsvc = parse(lhs);%0A        String rhs = e%5B1%5D;%0A        VarConst rhsvc = parse(rhs);%0A        int variable = lhsvc.variable - rhsvc.variable;%0A        int constant = rhsvc.constant - lhsvc.constant;%0A        if (variable == 0 && constant == 0) %7B%0A            return %22Infinite solutions%22;%0A        %7D else if (variable == 0 && constant != 0) %7B%0A            return %22No solution%22;%0A        %7D%0A        return %22x=%22 + (constant / variable
 );%0A    
+%7D%0A%0A
     
-System.out.println(prob.solveEquation(%22x+5-3+x=6+x-2%22)); // x=2%0A        System.out.println(prob.solveEquation(%22x=x%22)); // Infinite soluti
+private static VarConst parse(String eq) %7B%0A        VarConst varConst = new VarConst();%0A        int prev = 0;%0A        int curr = 0;%0A        for (; curr %3C eq.length(); curr++) %7B%0A            if (curr != 0 && (eq.charAt(curr) == '+' %7C%7C eq.charAt(curr) == '-')) %7B%0A                String sub = eq.substring(prev, curr);%0A                update(varConst, sub);%0A                prev = curr;%0A            %7D%0A        %7D%0A        String sub = eq.substring(prev, curr);%0A        update(varConst, sub);%0A        return varC
 ons
+t;
 %0A    
+%7D%0A%0A
     
-System.out.println(prob.solveEquation(%222x=x%22)); // x=0%0A        System.out.println(prob.solveEquation(%222x+3x-6x=x+2%22)); // x=
+private static void update(VarConst varConst, String sub) %7B%0A        if (sub.contains(%22x%22)) %7B%0A            sub = sub.replaceAll(%22x%22, %22%22);%0A            if (sub.isEmpty() %7C%7C sub.equals(%22+%22)) %7B%0A                varConst.variable += 1;%0A            %7D else if (sub.equals(%22-%22)) %7B%0A                varConst.variable += 
 -1
+;
 %0A   
@@ -1515,71 +1515,280 @@
     
-System.out.println(prob.solveEquation(%22x=x+2%22)); // No solution
+    %7D else %7B%0A                varConst.variable += Integer.parseInt(sub);%0A            %7D%0A        %7D else %7B%0A            varConst.constant += Integer.parseInt(sub);%0A        %7D%0A    %7D%0A%0A    private static class VarConst %7B%0A        private int variable;%0A        private int constant;
 %0A   

Solve problem 640