> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Solve the Equation Python Solution with Tests

> Tested Python solution for LeetCode 640 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 640, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Simulation](/catalog/topics/simulation), Linear Algebra. [View on LeetCode](https://leetcode.com/problems/solve-the-equation/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 640   # by problem number
lcpy gen -s solve_the_equation   # by problem name
```

## Problem

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.

### Examples

```
Input: equation = "x+5-3+x=6+x-2"
Output: "x=2"
```

```
Input: equation = "x=x"
Output: "Infinite solutions"
```

```
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'.
* The input is generated that if there is a single solution, it will be an integer.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/solve_the_equation/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/solve_the_equation/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def solve_equation(self, equation: str) -> str:
        left, right = equation.split("=")
        x_left, c_left = self._parse(left)
        x_right, c_right = self._parse(right)

        x_coeff = x_left - x_right
        const = c_right - c_left
        if x_coeff == 0:
            return "Infinite solutions" if const == 0 else "No solution"
        assert const % x_coeff == 0
        return f"x={const // x_coeff}"

    def _parse(self, side: str) -> tuple[int, int]:
        coeff = 0
        const = 0
        sign = 1
        num: int | None = None
        for ch in side:
            if ch.isdigit():
                num = (num if num is not None else 0) * 10 + int(ch)
            elif ch == "x":
                coeff += sign * (num if num is not None else 1)
                num = None
            else:
                if num is not None:
                    const += sign * num
                    num = None
                sign = -1 if ch == "-" else 1
        if num is not None:
            const += sign * num
        return coeff, const
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.