Solve the Equation

We’ll solve the given equation by parsing it into two parts: the left-hand side (LHS) and the right-hand side (RHS) of the equality. We’ll represent both sides as a linear equation of the form ( ax + b ), where ( a ) is the coefficient of ( x ), and ( b ) is the constant term. We’ll then solve for ( x ) by equating the two sides.

Here’s the step-by-step solution:

  1. Split the Equation: Split the equation into two parts, left and right of the equality sign.
  2. Parse LHS and RHS: Parse both sides separately to calculate the coefficients of ( x ) and the constant terms.
  3. Solve for ‘x’: Compare the coefficients of ( x ) and the constant terms of the LHS and RHS to solve for ( x ).

Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution:
    def solveEquation(self, equation: str) -> str:
        # Function to parse a side of the equation
        def parse(equation_part):
            sign = 1
            coefficient = 0
            constant = 0
            i = 0
            while i < len(equation_part):
                if equation_part[i] == '-':
                    sign = -1
                    i += 1
                elif equation_part[i] == '+':
                    sign = 1
                    i += 1

                if i < len(equation_part) and equation_part[i] == 'x':
                    coefficient += sign
                    i += 1
                else:
                    j = i
                    while j < len(equation_part) and equation_part[j].isdigit():
                        j += 1
                    if j > i:
                        num = int(equation_part[i:j])
                    else:
                        num = 1
                    if j < len(equation_part) and equation_part[j] == 'x':
                        coefficient += sign * num
                        j += 1
                    else:
                        constant += sign * num
                    i = j

            return coefficient, constant

        # Split equation into LHS and RHS
        lhs, rhs = equation.split('=')

        # Parse LHS and RHS
        lhs_coefficient, lhs_constant = parse(lhs)
        rhs_coefficient, rhs_constant = parse(rhs)

        # Calculate total coefficients and constants
        coefficient = lhs_coefficient - rhs_coefficient
        constant = rhs_constant - lhs_constant

        # Solve for x
        if coefficient == 0:
            if constant == 0:
                return "Infinite solutions"
            else:
                return "No solution"
        else:
            return "x=" + str(constant // coefficient)

The code first defines a function parse to calculate the coefficients and constants for either side of the equation. The main function then splits the equation, parses both sides, and solves for ‘x’. If there’s exactly one solution, it returns the value as “x=#value”; if there’s no solution, it returns “No solution”; if there are infinite solutions, it returns “Infinite solutions”.