LeetCode 640: Solve the Equation Solution

Master LeetCode problem 640 (Solve the Equation), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

640. Solve the Equation

Problem Explanation

Explanation

To solve this problem, we need to parse the equation and separate the coefficients of x and the constants on both sides of the equation. Then we simplify the equation by moving all x terms to one side and all constant terms to the other side. Finally, we calculate the value of x and handle cases where there are no solutions or infinite solutions.

Here is the step-by-step algorithm:

  1. Parse the equation to separate coefficients of x and constants on both sides.
  2. Simplify the equation by moving all x terms to one side and all constant terms to the other side.
  3. Calculate the value of x.
  4. Handle cases where there are no solutions or infinite solutions.

Time complexity: O(n) where n is the length of the equation.
Space complexity: O(1)

Solution Code

class Solution {
    public String solveEquation(String equation) {
        int xCoeff = 0, constant = 0;
        int sign = 1;
        int n = equation.length();
        int i = 0;

        while (i < n) {
            int j = i + 1;
            while (j < n && equation.charAt(j) != '+' && equation.charAt(j) != '-') {
                j++;
            }
            String term = equation.substring(i, j);
            if (term.contains("x")) {
                if (term.equals("x") || term.equals("+x")) {
                    xCoeff += sign;
                } else if (term.equals("-x")) {
                    xCoeff -= sign;
                } else {
                    xCoeff += sign * Integer.parseInt(term.substring(0, term.length() - 1));
                }
            } else {
                constant += sign * Integer.parseInt(term);
            }

            if (j < n && equation.charAt(j) == '-') {
                sign = -1;
            } else {
                sign = 1;
            }
            i = j + 1;
        }

        if (xCoeff == 0) {
            if (constant == 0) {
                return "Infinite solutions";
            } else {
                return "No solution";
            }
        } else {
            return "x=" + (-constant / xCoeff);
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 640 (Solve the Equation)?

This page provides optimized solutions for LeetCode problem 640 (Solve the Equation) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 640 (Solve the Equation)?

The time complexity for LeetCode 640 (Solve the Equation) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 640 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 640 in Java, C++, or Python.

Back to LeetCode Solutions