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.
Example 1:
Input: equation = "x+5-3+x=6+x-2" Output: "x=2"
Example 2:
Input: equation = "x=x" Output: "Infinite solutions"
Example 3:
Input: equation = "2x=x" Output: "x=0"
Constraints:
3 <= equation.length <= 1000
equation
has exactly one '='
.equation
consists of integers with an absolute value in the range [0, 100]
without any leading zeros, and the variable 'x'
.This problem involves solving a linear equation with one variable, 'x'. The equation is given as a string, and the solution needs to handle three possible outcomes: a unique integer solution for 'x', infinitely many solutions, or no solution.
The core idea is to parse the equation string, separate the left and right-hand sides, and then collect the coefficients of 'x' and the constant terms. The solution then uses these coefficients to determine the solution.
Parsing the Equation: The input string is split into two parts at the '=' sign, representing the left-hand side (LHS) and the right-hand side (RHS) of the equation.
Parsing Each Side: A helper function (f
in the code examples) is used to parse each side (LHS and RHS). This function iterates through the string, identifying terms (numbers and 'x' terms) and their signs (+ or -). It keeps track of the coefficient of 'x' and the constant term separately. For example in "2x+3", the x coefficient is 2 and the constant is 3. In "-x-4", the x coefficient is -1 and the constant is -4.
Solving for x: Once the coefficients of 'x' (x1 and x2) and the constant terms (y1 and y2) are obtained for the LHS and RHS respectively, we can form the equation: x1*x + y1 = x2*x + y2
. This can be rearranged to solve for x: x = (y2 - y1) / (x1 - x2)
.
Handling Special Cases:
x1 - x2
is 0, it means there's either an infinite number of solutions (if y1
and y2
are equal), or no solution (if y1
and y2
are unequal).Returning the Result: The function returns the solution in the format "x=#value", or "Infinite solutions", or "No solution" as appropriate.
Time Complexity: The time complexity is dominated by the parsing of the input string. The parsing process iterates through the string once for the LHS and once for the RHS. Therefore, the time complexity is O(n), where n is the length of the input string.
Space Complexity: The space complexity is O(1) because the algorithm uses a fixed amount of extra space to store the coefficients and constants, regardless of the input size. The space used does not grow with the size of the input.
The code examples provided in Python, Java, Go, and TypeScript implement the approach described above. The comments in the code explain each step in detail. Note that the helper function f
is crucial for the parsing aspect of the solution. This function extracts the coefficient of x and the constant from each side of the equation.
The core logic remains consistent across the languages; only the syntax varies slightly. This demonstrates the underlying algorithm's adaptability and clarity.