forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve-the-equation.cpp
56 lines (50 loc) · 1.71 KB
/
solve-the-equation.cpp
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
// Time: O(n)
// Space: O(n)
class Solution {
public:
string solveEquation(string equation) {
auto a = 0, b = 0;
auto side = 1;
int submatches[] = { 1, 2, 3, 4 };
auto e = regex("(=)|([-+]?)(\\d*)(x?)");
for (regex_token_iterator<string::iterator> it(equation.begin(), equation.end(), e, submatches), end;
it != end;) {
auto eq = (it++)->str();
auto sign = (it++)->str();
auto num = (it++)->str();
auto isx = (it++)->str();
if (!eq.empty()) {
side = -1;
} else if (!isx.empty()) {
a += side * stoi(sign + "1") * stoi(num.empty() ? "1" : num);
} else if (!num.empty()) {
b -= side * stoi(sign + num);
}
}
return a != 0 ? "x=" + to_string(b / a) : b != 0 ? "No solution" : "Infinite solutions";
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
string solveEquation(string equation) {
equation = regex_replace(equation, regex("(^|[+=-])x"), "$011x");
auto pos = equation.find('=');
auto l = coef(equation.substr(0, pos));
auto r = coef(equation.substr(pos + 1));
auto a = l.first - r.first;
auto b = r.second - l.second;
return a != 0 ? "x=" + to_string(b / a) : b != 0 ? "No solution" : "Infinite solutions";
}
private:
pair<int, int> coef(string s) {
auto a = 0, b = 0;
auto e = regex("(^|[+-])\\d+x?");
for (regex_token_iterator<string::iterator> it(s.begin(), s.end(), e), end;
it != end; ++it) {
(it->str().back() == 'x' ? a : b) += stoi(*it);
}
return {a, b};
}
};