Solve the Equation Solution in Java

Ah, the classic “Solve the Equation” problem! It’s like trying to figure out who ate the last slice of pizza at a party—everyone has their theories, but only one person is guilty (or in this case, only one variable, x, is the answer). In this delightful little puzzle, you’re tasked with solving a linear equation of the form ax + b = cx + d. Sounds simple, right? Well, it can get a bit tricky, especially when you throw in some constants and coefficients that like to play hide and seek.

Imagine you’re at a family gathering, and your uncle insists that he can solve any equation faster than you can say “Pythagorean theorem.” You roll your eyes and think, “Sure, Uncle Bob, but can you handle the complexities of coefficients and constants?” Spoiler alert: he can’t.

Language Links


Code Solution


class Solution {
  public String solveEquation(String equation) {
    String[] equations = equation.split("=");
    int[] lhs = calculate(equations[0]);
    int[] rhs = calculate(equations[1]);
    int coefficient = lhs[0] - rhs[0];
    int constant = rhs[1] - lhs[1];

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

  private int[] calculate(final String s) {
    int coefficient = 0;
    int constant = 0;
    int num = 0;
    int sign = 1;

    for (int i = 0; i < s.length(); ++i) {
      char c = s.charAt(i);
      if (Character.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.charAt(i - 1) == '0')
          continue;
        coefficient += num == 0 ? sign : sign * num;
        num = 0;
      }
    }

    return new int[] {coefficient, constant + sign * num};
  }
}
    

Approach Explanation

The code above breaks down the equation into two parts: the left-hand side (LHS) and the right-hand side (RHS). It calculates the coefficients and constants for both sides and then compares them. If the coefficients are equal and the constants are also equal, you have infinite solutions (like how many times you can eat pizza). If the coefficients are equal but the constants differ, there’s no solution (just like how you can’t divide a pizza among more people than there are slices). Otherwise, it calculates the value of x.

Time and Space Complexity

Complexity Type Complexity
Time Complexity O(n), where n is the length of the equation string.
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 balance your budget. You have a fixed income (the coefficient) and various expenses (the constants). If your income equals your expenses, you’re living the dream! But if your expenses exceed your income, it’s time to cut back on those fancy lattes. This problem is just like that—finding the balance between what you have and what you owe.

Similar Problems

If you enjoyed this problem, you might want to check out these related challenges: