-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
35 lines (35 loc) · 956 Bytes
/
solution.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
/**
* 113 / 113 test cases passed.
* Runtime: 0 ms
* Memory Usage: 6.2 MB
*/
class Solution {
public:
bool backspaceCompare(string s, string t) {
int p = s.size() - 1, q = t.size() - 1;
int p_back = 0, q_back = 0;
while (p >= 0 || q >= 0) {
while (p >= 0) {
if (s[p] == '#') {
++ p_back, -- p;
} else if (p_back > 0) {
-- p_back, -- p;
} else break;
}
while (q >= 0) {
if (t[q] == '#') {
++ q_back, -- q;
} else if (q_back > 0) {
-- q_back, -- q;
} else break;
}
if (p >= 0 && q >= 0) {
if (s[p] != t[q]) return false;
} else if (p >= 0 || q >= 0) {
return false;
}
-- p, -- q;
}
return true;
}
};