Class Solution

java.lang.Object
g0601_0700.s0640_solve_the_equation.Solution

public class Solution extends Object
640 - Solve the Equation.<p>Medium</p> <p>Solve a given equation and return the value of <code>'x'</code> in the form of a string <code>&quot;x=#value&quot;</code>. The equation contains only <code>'+'</code>, <code>'-'</code> operation, the variable <code>'x'</code> and its coefficient. You should return <code>&quot;No solution&quot;</code> if there is no solution for the equation, or <code>&quot;Infinite solutions&quot;</code> if there are infinite solutions for the equation.</p> <p>If there is exactly one solution for the equation, we ensure that the value of <code>'x'</code> is an integer.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> equation = &ldquo;x+5-3+x=6+x-2&rdquo;</p> <p><strong>Output:</strong> &ldquo;x=2&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> equation = &ldquo;x=x&rdquo;</p> <p><strong>Output:</strong> &ldquo;Infinite solutions&rdquo;</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> equation = &ldquo;2x=x&rdquo;</p> <p><strong>Output:</strong> &ldquo;x=0&rdquo;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 <= equation.length <= 1000</code></li> <li><code>equation</code> has exactly one <code>'='</code>.</li> <li><code>equation</code> consists of integers with an absolute value in the range <code>[0, 100]</code> without any leading zeros, and the variable <code>'x'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • solveEquation

      public String solveEquation(String equation)