# Very simple Java solution with full comments

• ``````/*
* Algorithm:
* split the equation to two side
*
* try all x from -1000 to 1000 and calc value of each side
*
* if result of two side is equal, then totalAnswer increase
*
* if totalAnswer == 0 mean No solution
* if totalAnswer > 1 mean Infinite solutions
* */
public String solveEquation(String equation) {
// split equation to two side
String[] side = equation.split("=");

// try all x from -1000 to 1000
for (int x = -1000; x <= 1000; x++) {
if (calc(x, side[0]) == calc(x, side[1])) {
}
}

return "No solution";
}

return "Infinite solutions";
}

}

private long calc(int x, String s) {
int idx = 0;
char c = '+';
boolean haveBefore;

while (idx < s.length()) {
long val = 0;
haveBefore = false;

// try to parse string to int
// example: "21" to 21, "2x" to 2 * x, "x" to x
while (idx < s.length() && !"+-".contains(s.charAt(idx) + "")) {
if (s.charAt(idx) != 'x') {
val *= 10;
val += s.charAt(idx) - '0';
haveBefore = true;
} else {
if (haveBefore) {
val *= x;
} else {
val = x;
}
}
idx++;
}

// sum val to final answer
if (c == '+') {
} else {
}

if (idx < s.length()) {
c = s.charAt(idx);
idx++;
}
}

}
``````

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