-
Notifications
You must be signed in to change notification settings - Fork 820
/
SolveTheEquation.java
106 lines (99 loc) · 2.93 KB
/
SolveTheEquation.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* (C) 2024 YourCompanyName */
package math;
/**
* Created by gouthamvidyapradhan on 16/02/2018. Solve a given equation and return the value of x in
* the form of string "x=#value". The equation contains only '+', '-' operation, the variable x and
* its coefficient.
*
* <p>If there is no solution for the equation, return "No solution".
*
* <p>If there are infinite solutions for the equation, return "Infinite solutions".
*
* <p>If there is exactly one solution for the equation, we ensure that the value of x is an
* integer.
*
* <p>Example 1: Input: "x+5-3+x=6+x-2" Output: "x=2" Example 2: Input: "x=x" Output: "Infinite
* solutions" Example 3: Input: "2x=x" Output: "x=0" Example 4: Input: "2x+3x-6x=x+2" Output: "x=-1"
* Example 5: Input: "x=x+2" Output: "No solution"
*
* <p>Solution: Solve the left and right part separately and then sum up the results.
*/
public class SolveTheEquation {
int xL = 0, xR = 0, tL = 0, tR = 0;
public static void main(String[] args) throws Exception {
System.out.println(new SolveTheEquation().solveEquation("x=x+2"));
}
public String solveEquation(String equation) {
String[] parts = equation.split("=");
solve(parts[0], true);
solve(parts[1], false);
long right = (long) tR - tL;
long left = (long) xL - xR;
if (left == 0 && right == 0) {
return "Infinite solutions";
} else if (left == 0) {
return "No solution";
} else if (right == 0) {
return "x=0";
} else {
return "x=" + (right / left);
}
}
private void solve(String s, boolean isLeft) {
String num = "";
int xSum = 0;
int rest = 0;
boolean isNeg = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '-') {
if (!num.isEmpty()) {
xSum = calculate(num, isNeg, xSum, rest)[0];
rest = calculate(num, isNeg, xSum, rest)[1];
}
isNeg = true;
num = "";
} else if (c == '+') {
if (!num.isEmpty()) {
xSum = calculate(num, isNeg, xSum, rest)[0];
rest = calculate(num, isNeg, xSum, rest)[1];
}
isNeg = false;
num = "";
} else {
num += c;
}
}
if (!num.isEmpty()) {
xSum = calculate(num, isNeg, xSum, rest)[0];
rest = calculate(num, isNeg, xSum, rest)[1];
}
if (isLeft) {
xL = xSum;
tL = rest;
} else {
xR = xSum;
tR = rest;
}
}
private int[] calculate(String num, boolean isNeg, int xSum, int rest) {
int[] A = new int[2];
if (num.contains("x")) {
num = num.substring(0, num.length() - 1);
if (isNeg) {
xSum -= num.isEmpty() ? 1 : Integer.parseInt(num);
} else {
xSum += num.isEmpty() ? 1 : Integer.parseInt(num);
}
} else {
if (isNeg) {
rest -= Integer.parseInt(num);
} else {
rest += Integer.parseInt(num);
}
}
A[0] = xSum;
A[1] = rest;
return A;
}
}