Solve The Equation Problem


Description

LeetCode Problem 640.

Solve a given equation and return the value of ‘x’ in the form of a string “x=#value”. The equation contains only ‘+’, ‘-‘ operation, the variable ‘x’ and its coefficient. You should return “No solution” if there is no solution for the equation, or “Infinite solutions” if there are infinite solutions for the equation.

If there is exactly one solution for the equation, we ensure that the value of ‘x’ is an integer.

Example 1:

1
2
Input: equation = "x+5-3+x=6+x-2"
Output: "x=2"

Example 2:

1
2
Input: equation = "x=x"
Output: "Infinite solutions"

Example 3:

1
2
Input: equation = "2x=x"
Output: "x=0"

Example 4:

1
2
Input: equation = "2x+3x-6x=x+2"
Output: "x=-1"

Example 5:

1
2
Input: equation = "x=x+2"
Output: "No solution"

Constraints:

  • 3 <= equation.length <= 1000
  • equation has exactly one ‘=’.
  • equation consists of integers with an absolute value in the range [0, 100] without any leading zeros, and the variable ‘x’.


Sample C++ 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
class Solution {
public:
    string solveEquation(string equation) {
        int i = 0;
        int para = 0, xpara = 0;
        int flag = 1;
        while (i < equation.size()) {
            int sign = 1;
            int temp = 0;
            if (equation[i] == '=') {
                flag = -1;
                i ++;
            }
            if (equation[i] == '-') {
                sign = -1; 
                i ++;
            }
            if (equation[i] == '+') {
                sign = 1;
                i ++;
            }
            
            if (isdigit(equation[i])) {
                while (i < equation.size() && isdigit(equation[i])) {
                    temp = temp * 10 + equation[i] - '0';
                    i ++;
                }
                if (i < equation.size() && equation[i] == 'x') {
                    xpara += flag * sign * temp;
                    i ++;
                } 
                else 
                    para += flag * sign * temp;
            } 
            else {
                xpara += flag * sign;
                i ++;
            }
            
        }
        
        string res;
        if (para == 0 && xpara == 0) 
            res = "Infinite solutions";
        else if (xpara == 0) 
            res = "No solution";
        else
            res = "x=" + to_string(para / xpara * -1);
        
        return res;
    }
};




Related Posts

String Without Aaa Or Bbb Problem

LeetCode 984. Given two integers a and b, return any...

Shifting Letters Problem

LeetCode 848. You are given a string s of lowercase...

Positions Of Large Groups Problem

LeetCode 830. In a string sof lowercase letters, these letters...

Orderly Queue Problem

LeetCode 899. You are given a string s and an...

Number Of Lines To Write String Problem

LeetCode 806. You are given a string s of lowercase...

Masking Personal Information Problem

LeetCode 831. You are given a personal information string s,...