# Solve the Equation LeetCode Solution

Here, We see Solve the Equation LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

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:
Input: equation = “x+5-3+x=6+x-2”
Output: “x=2”

Example 2:
Input: equation = “x=x”
Output: “Infinite solutions”

Example 3:
Input: equation = “2x=x”
Output: “x=0”

## Solve the Equation LeetCodeSolutionC++

``````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<int, int> 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};
}
};```Code language: PHP (php)```

## Solve the Equation LeetCodeSolutionJava

``````class Solution {
public String solveEquation(String equation) {
int x1 = 0,x2 = 0;
int sum1=0,sum2=0;
int sign = 1;
int i=0;
boolean flag = true;
for(i=0;i<equation.length();i++){
char ch = equation.charAt(i);
if(ch == '=') flag = false;
else if(ch == 'x'){
if(flag) x1 += sign;
else x2 += sign;
sign = 1;
}
else if(Character.isDigit(ch)){
int val =0;
while(i<equation.length() && Character.isDigit(equation.charAt(i))){
val = val*10 + Character.getNumericValue(equation.charAt(i));
i++;
}
i--;
if(i<equation.length()-1 && equation.charAt(i+1) == 'x'){
i++;
if(flag) x1+=sign * val;
else x2 += sign * val;
}
else{
if(flag)sum1 += sign * val;
else sum2 += sign * val;
}
sign = 1;
}else if(ch == '-') sign = -1;
}
StringBuilder sb = new StringBuilder();
if(x1==x2 && sum1 == sum2) return "Infinite solutions";
else if(sum1 == 0 && sum2 == 0) return "x=0";
else if(x1 == x2 && ((sum1!=0)||(sum2!=0))) return "No solution";
int sum=0;
int x=0;
if(x1>x2){
x = x1 - x2;
sum = sum2-sum1;
sum = sum/x;
}
else if(x2>x1){
x = x2 - x1;
sum = sum1-sum2;
sum = sum/x;
}
sb.append("x=");
sb.append(sum);
return sb.toString();
}
}```Code language: JavaScript (javascript)```

## Solve the Equation LeetCodeSolutionJavaScript

``````var solveEquation = function(equation) {
const [left, right] = equation.split('=');
const [lsv,lsc] = solve(left);
const [rsv,rsc] = solve(right);
const [variable,constant] = [lsv-rsv,rsc-lsc];
return !variable ? (lsc === rsc ? "Infinite solutions":
"No solution") : `x=\${constant / variable}`
};

const solve = equation => {
let x = 0;
let num = 0;
for(let i = 0; i < equation.length; i++){
if(equation[i] === "x"){
if(equation[i - 1] === "+") x += 1;
else if(equation[i - 1] === "-") x -= 1;
else x = 1;
}else if(!isNaN(equation[i])){
let str = equation[i];
let k = i + 1;
while(!isNaN(equation[k])){
str += equation[k];
k++;
}
let res = equation[k] === "x" ? x : num;
if(equation[i - 1] === "-") res -= parseInt(str)
else if(equation[i - 1] === "+") res += parseInt(str);
else res = parseInt(str)
if(equation[k] === "x") x = res;
else num = res;
i = k;
}
}
return [x,num]
}```Code language: PHP (php)```

## Solve the Equation LeetCodeSolutionPython

``````class Solution(object):
def solveEquation(self, equation):
x = a = 0
side = 1
for eq, sign, num, isx in re.findall('(=)|([-+]?)(\d*)(x?)', equation):
if eq:
side = -1
elif isx:
x += side * int(sign + '1') * int(num or 1)
elif num:
a -= side * int(sign + num)
return 'x=%d' % (a / x) if x else 'No solution' if a else 'Infinite solutions'``````
Scroll to Top