Simple Java Solution with comments


  • 0
    A
    
    public class Solution {
        public String solveEquation(String equation) {
            int l = equation.length(), lx = 0, rx = 0, lv = 0, rv = 0;
            boolean flag = true; // we'll calculate lx and lv
            char[] stc = equation.toCharArray();
            for (int i = 0; i < l; i++) { // lhs or rhs can be ax + b(constant) where a and b(constant) can be positive or negative integer
                char c = stc[i];
                boolean check = false; // this flag is used to to check negative constants or Xs
                if (c == '=') {
                    flag = false; // now we'll calculate rx and rv
                    continue;
                }
                if (c == '+') {
                    continue;
                } else if (c == 'x') { // we got x so we add 1 to lx or rx depending on flag
                    if (flag)
                        lx++;
                    else
                        rx++;
                } else { // we got ax or constant where a and b(constant) can be positive or negative integer
                    int j = i;
                    if (c == '-') {
                        check = true; // set the negative flag to true
                        j++;
                    }
                    if (check && stc[j] == 'x') { //we got -x so we substract 1 from lx or rx depending on flag
                        if (flag)
                            lx--;
                        else
                            rx--;
                        i = j;
                        continue;
                    } else { 
                        StringBuilder sb = new StringBuilder("");
                        while (j < l && Character.isDigit(stc[j])) {
                            sb.append(stc[j++]);
                        }
                        int n = Integer.parseInt(sb.toString());// we got the a or b (number) where a is coefficient of x
                        if (j < l) {
                            if (stc[j] == 'x') {
                                if (check) {
                                    if (flag)
                                        lx -= n;
                                    else
                                        rx -= n;
                                } else {
                                    if (flag)
                                        lx += n;
                                    else
                                        rx += n;
                                }
                                i = j;
                            } else { // so next char is either + or - so we got the number(b)
                            // so add or substrct the number depending on the flag(lhs or rhs) and the check boolean
                                if (check) {
                                    if (flag)
                                        lv -= n;
                                    else
                                        rv -= n;
                                } else {
                                    if (flag)
                                        lv += n;
                                    else
                                        rv += n;
                                }
                                i = j - 1;
                            }
                        } else {// if we reached the end of the string then it can not be ax as after number(n) we reached end, so its  b(number)
                            // so add or substrct the number depending on the flag(lhs or rhs) and the check boolean
                            if (check) {
                                if (flag)
                                    lv -= n;
                                else
                                    rv -= n;
                            } else {
                                if (flag)
                                    lv += n;
                                else
                                    rv += n;
                            }
                            i = j - 1;
                        }
    
                    }
                }
            }
            // now we have lx,lv,rx and rv
            String ans = "";
            if (lx == rx) {
                if (lv == rv) {
                    ans = "Infinite solutions";
                } else {
                    ans = "No solution";
                }
            } else {
                int bx = lx - rx;
                int v = rv - lv;
    
                ans += "x=";
                v /= bx;
                ans += v;
            }
            return ans;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.