|
1 | 1 | class Solution { |
2 | | - public String solveEquation(String equation) { |
3 | | - int leftXCount = 0; |
4 | | - int rightXCount = 0; |
5 | | - int leftNumCount = 0; |
6 | | - int rightNumCount = 0; |
7 | | - boolean switchDone = false; |
8 | | - int sign = 1; |
9 | | - StringBuilder sb = new StringBuilder(); |
10 | | - |
11 | | - for (char c : equation.toCharArray()) { |
12 | | - if (c == '=') { |
13 | | - if (sb.length() > 0) { |
14 | | - leftNumCount += sign * Integer.parseInt(sb.toString()); |
15 | | - sb.setLength(0); |
16 | | - sign = 1; |
17 | | - } |
18 | | - |
19 | | - switchDone = true; |
20 | | - continue; |
21 | | - } |
22 | | - |
23 | | - if (c == 'x') { |
24 | | - if (switchDone) { |
25 | | - rightXCount += sign * (sb.length() > 0 ? Integer.parseInt(sb.toString()) : 1); |
26 | | - } |
27 | | - else { |
28 | | - leftXCount += sign * (sb.length() > 0 ? Integer.parseInt(sb.toString()) : 1); |
29 | | - } |
30 | | - |
31 | | - sign = 1; |
32 | | - sb.setLength(0); |
33 | | - } |
34 | | - else if (Character.isDigit(c)) { |
35 | | - sb.append(c); |
36 | | - } |
37 | | - else if (c == '+' || c == '-') { |
38 | | - if (sb.length() > 0) { |
39 | | - if (switchDone) { |
40 | | - rightNumCount += sign * (Integer.parseInt(sb.toString())); |
41 | | - } |
42 | | - else { |
43 | | - leftNumCount += sign * (Integer.parseInt(sb.toString())); |
44 | | - } |
45 | | - |
46 | | - sb.setLength(0); |
47 | | - } |
48 | | - |
49 | | - sign = c == '+' ? 1 : -1; |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - if (sb.length() > 0) { |
54 | | - rightNumCount += sign * Integer.parseInt(sb.toString()); |
55 | | - } |
56 | | - |
57 | | - if (leftXCount == rightXCount && leftNumCount != rightNumCount) { |
58 | | - return "No solution"; |
59 | | - } |
60 | | - else if (leftXCount == rightXCount && leftNumCount == rightNumCount) { |
61 | | - return "Infinite solutions"; |
62 | | - } |
63 | | - else { |
64 | | - int totalXCount = leftXCount - rightXCount; |
65 | | - int totalValueCount = rightNumCount - leftNumCount; |
66 | | - |
67 | | - int gcd = getGcd(totalXCount, totalValueCount); |
68 | | - if (gcd != 0) { |
69 | | - totalXCount /= gcd; |
70 | | - totalValueCount /= gcd; |
71 | | - } |
72 | | - |
73 | | - if (totalXCount < 0) { |
74 | | - totalXCount *= -1; |
75 | | - totalValueCount *= -1; |
76 | | - } |
77 | | - |
78 | | - return new StringBuilder() |
79 | | - .append(totalXCount > 1 ? totalXCount : "") |
80 | | - .append("x=") |
81 | | - .append(totalValueCount) |
82 | | - .toString(); |
83 | | - } |
| 2 | + public String solveEquation(String equation) { |
| 3 | + int[] equationFirst = parseEquation(equation.split("=")[0]); |
| 4 | + int[] equationSecond = parseEquation(equation.split("=")[1]); |
| 5 | + int constantPart = equationSecond[1] - equationFirst[1]; |
| 6 | + int variablePart = equationFirst[0] - equationSecond[0]; |
| 7 | + if (variablePart == 0) { |
| 8 | + return constantPart == 0 ? "Infinite solutions" : "No solution"; |
84 | 9 | } |
85 | | - |
86 | | - private int getGcd(int totalXCount, int totalValueCount) { |
87 | | - if (totalXCount == 0) { |
88 | | - return totalValueCount; |
| 10 | + int sign = (variablePart < 0 && constantPart < 0) || (variablePart > 0 && constantPart > 0) ? 1 : -1; |
| 11 | + if (Math.abs(constantPart) % Math.abs(variablePart) == 0) { |
| 12 | + constantPart = Math.abs(constantPart) / Math.abs(variablePart); |
| 13 | + variablePart = 1; |
| 14 | + } |
| 15 | + return (variablePart > 1 ? variablePart : "") + "x=" + sign * constantPart; |
| 16 | + } |
| 17 | + |
| 18 | + private int[] parseEquation(String s) { |
| 19 | + int variablePart = 0; |
| 20 | + int constantPart = 0; |
| 21 | + int sign = 1; |
| 22 | + int n = s.length(); |
| 23 | + for (int idx = 0; idx < n;) { |
| 24 | + if (s.charAt(idx) == 'x') { |
| 25 | + variablePart += sign; |
| 26 | + idx++; |
| 27 | + sign = 1; |
| 28 | + } else if (Character.isDigit(s.charAt(idx))) { |
| 29 | + int num = 0; |
| 30 | + while (idx < n && Character.isDigit(s.charAt(idx))) { |
| 31 | + num = num * 10 + Character.getNumericValue(s.charAt(idx++)); |
| 32 | + } |
| 33 | + if (idx != n && s.charAt(idx) == 'x') { |
| 34 | + variablePart += sign * num; |
| 35 | + idx++; |
| 36 | + } else { |
| 37 | + constantPart += sign * num; |
89 | 38 | } |
90 | | - |
91 | | - return getGcd(totalValueCount % totalXCount, totalXCount); |
| 39 | + sign = 1; |
| 40 | + } else { |
| 41 | + sign = s.charAt(idx++) == '+' ? 1 : -1; |
| 42 | + } |
92 | 43 | } |
| 44 | + return new int[]{variablePart, constantPart}; |
| 45 | + } |
93 | 46 | } |
0 commit comments