Skip to content

Commit c1b6fa5

Browse files
authored
Create Reversing the equation.cpp
1 parent 519aaf3 commit c1b6fa5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Reversing the equation.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
GFG
2+
Reversing the equation
3+
4+
class Solution {
5+
public:
6+
string reverseEqn(string s) {
7+
string reversed;
8+
int equationStart = s.length() - 1;
9+
10+
for (int i = s.length() - 1; i >= 0; i--) {
11+
if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
12+
reversed += s.substr(i + 1, equationStart - i) + s[i];
13+
equationStart = i - 1;
14+
}
15+
}
16+
17+
reversed += s.substr(0, equationStart + 1);
18+
19+
return reversed;
20+
}
21+
};

0 commit comments

Comments
 (0)