#640
Solve the Equation
specialist · 995 · lc medium +32 · verified · 46.1% accepted · 547 likes · top 30%
Description
Given a linear equation string involving 'x', constants, '+', and '-', solve for x and return "x=#value". If no value of x satisfies the equation, return "No solution". If every value works, return "Infinite solutions". When a unique integer solution exists, return it in the format "x=#value".
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"
Code
1
2
3