Skip to main content
LeetCode 640, Medium. Topics: Math, String, Simulation, Linear Algebra. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026