Solve the Equation Solution in C++

Navigate to Other Solutions

Problem Description

Welcome to the world of equations, where numbers dance and variables play hide and seek! The problem at hand is to solve a simple linear equation in one variable. You know, the kind of equation that makes you question your life choices while staring at a math book.

Imagine you’re at a party, and everyone is talking about their favorite snacks. You want to join in, but you can only talk about snacks that can be expressed in the form of an equation. For example, if someone says, “I have 2x + 3 = 7,” you need to figure out how many snacks (x) they actually have. Spoiler alert: it’s not as easy as it sounds!

The task is to take an equation in the form of a string, split it into left-hand side (LHS) and right-hand side (RHS), and then solve for x. If you find that x can take on multiple values, you’ll declare “Infinite solutions.” If it’s impossible to find a value for x, you’ll say “No solution.” Otherwise, you’ll provide the value of x.

Code Solution


class Solution {
 public:
  string solveEquation(string equation) {
    const string lhsEquation = equation.substr(0, equation.find('='));
    const string rhsEquation = equation.substr(equation.find('=') + 1);
    const auto& [lhsCoefficient, lhsConstant] = calculate(lhsEquation);
    const auto& [rhsCoefficient, rhsConstant] = calculate(rhsEquation);
    const int coefficient = lhsCoefficient - rhsCoefficient;
    const int constant = rhsConstant - lhsConstant;

    if (coefficient == 0 && constant == 0)
      return "Infinite solutions";
    if (coefficient == 0 && constant != 0)
      return "No solution";
    return "x=" + to_string(constant / coefficient);
  }

 private:
  pair calculate(const string& s) {
    int coefficient = 0;
    int constant = 0;
    int num = 0;
    int sign = 1;

    for (int i = 0; i < s.length(); ++i) {
      const char c = s[i];
      if (isdigit(c))
        num = num * 10 + (c - '0');
      else if (c == '+' || c == '-') {
        constant += sign * num;
        sign = c == '+' ? 1 : -1;
        num = 0;
      } else {
        if (i > 0 && num == 0 && s[i - 1] == '0')
          continue;
        coefficient += num == 0 ? sign : sign * num;
        num = 0;
      }
    }

    return {coefficient, constant + sign * num};
  }
};

Approach Explanation

The code works by first splitting the equation into its left-hand side (LHS) and right-hand side (RHS). It then calculates the coefficients and constants for both sides using the calculate function. The coefficients are subtracted to find the net coefficient, and the constants are subtracted to find the net constant. Depending on the values of these two results, it determines if there are infinite solutions, no solution, or a specific value for x.

Time and Space Complexity

  • Time Complexity: O(n), where n is the length of the equation string. This is because we traverse the string to parse the coefficients and constants.
  • Space Complexity: O(1), as we are using a fixed amount of space for variables regardless of the input size.

Real-World Example

Let’s say you’re trying to figure out how many pizzas you can buy with your budget. If you have an equation like “3x + 2 = 14,” you can think of x as the number of pizzas. By solving the equation, you can determine how many pizzas you can afford. If you find that you can buy an infinite number of pizzas (because your budget is limitless), you’d be the life of the party!

Similar Problems

If you enjoyed this problem, you might also like these: