Solve a given equation and return the value of 'x' in the form of a string "x=#value". The equation contains only '+', '-' operation, the variable 'x' and its coefficient. You should return "No solution" if there is no solution for the equation, or "Infinite solutions" if there are infinite solutions for the equation.
If there is exactly one solution for the equation, we ensure that the value of 'x' is an integer.
equation consists of integers with an absolute value in the range [0, 100] without any leading zeros, and the variable 'x'.
The input is generated that if there is a single solution, it will be an integer.
Solutions
Solution 1: Mathematics
Thinking
A linear equation must be reduced to \(ax+b=0\). Coefficients may be omitted and signs abut, so an ad-hoc scan is error-prone.
Split on =. On each side, scan signed terms: a trailing x updates the coefficient, otherwise the constant. Compare both sides to report infinite, none, or the unique integer root.
We split the \(equation\) by the equal sign "=" into left and right expressions, and compute the coefficient of "x" (denoted \(x_i\)) and the constant value (denoted \(y_i\)) for each side.
The equation is then transformed into: \(x_1 \times x + y_1 = x_2 \times x + y_2\).
When \(x_1 = x_2\): if \(y_1 \neq y_2\), there is no solution; if \(y_1 = y_2\), there are infinite solutions.
When \(x_1 \neq x_2\): there is a unique solution \(x = \frac{y_2 - y_1}{x_1 - x_2}\).