Backspace String Compare LeetCode solution in C++

1
class Solution {
2
public:
3
bool backspaceCompare(string s, string t) {
4
stack<int> st1;
5
stack<int> st2;
6
int len = s.length();
7
int len2 = t.length();
8

9
for (int i = 0; i < len; i++) {
10
if (s[i] != '#') {
11
st1.push(s[i]);
12
} else {
13
if (!st1.empty()) {
14
st1.pop();
15
}
16
}
17
}
18
for (int i = 0; i < len2; i++) {
19
if (t[i] != '#') {
20
st2.push(t[i]);
21
} else {
22
if (!st2.empty()) {
23
st2.pop();
24
}
25
}
26
}
27

28
while (!st1.empty() && !st2.empty()) {
29
if (st1.top() != st2.top()) {
30
return false;
31
}
32
st1.pop();
33
st2.pop();
34
}
35
if (st1.empty() && st2.empty()) {
36
return true;
37
}
38
return false;
39
}
40
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0