-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec6_8.cpp
More file actions
49 lines (40 loc) · 1.02 KB
/
lec6_8.cpp
File metadata and controls
49 lines (40 loc) · 1.02 KB
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
class Solution {
public:
bool backspaceCompare(string s, string t) {
stack<char> st;
// Process string s
for(int i = 0; i < s.size(); i++) {
if(s[i] == '#') {
if(!st.empty()) {
st.pop();
}
} else {
st.push(s[i]);
}
}
string finalS = "";
while(!st.empty()) {
finalS += st.top();
st.pop();
}
stack<char> st1;
// Process string t
for(int i = 0; i < t.size(); i++) {
if(t[i] == '#') {
if(!st1.empty()) {
st1.pop();
}
} else {
st1.push(t[i]);
}
}
string finalT = "";
while(!st1.empty()) {
finalT += st1.top();
st1.pop();
}
// Compare the final processed strings
return finalS == finalT;
}
};
//