-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversing the equation.cpp
54 lines (52 loc) · 1.41 KB
/
Reversing 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
Problem Statement: https://practice.geeksforgeeks.org/problems/reversing-the-equation2205/1/?page=2&difficulty[]=0&category[]=Stack&sortBy=submissions#
Time Complexity: o(n)
Space Complexity: o(n) --space time taken by stack
string reverseEqn (string s)
{
//code here.
stack<string> a;
for(int i=0;i<s.length();i++)
{
string res="";
if(s[i]>=48 and s[i]<=57)
{
while( s[i]>=48 and s[i]<=57)
{
string p(1,s[i]);
res+=p;
i++;
}
a.push(res);
i--;
}
else if(s[i]=='+')
{
a.push("+");
}
else if(s[i]=='-')
{
a.push("-");
}
else if(s[i]=='*')
{
a.push("*");
}
else if(s[i]=='/')
{
a.push("/");
}
else
{
string p(1,s[i]);
res+=p;
a.push(res);
}
}
string res="";
while(a.size()!=0)
{
res+=a.top();
a.pop();
}
return res;
}