-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
47 lines (44 loc) · 1.18 KB
/
Copy pathmain.cpp
File metadata and controls
47 lines (44 loc) · 1.18 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
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
bool backspaceCompare(string s, string t) {
int size = s.size();
for (int i = 0; i < size; ++i) {
if (s.empty()) break;
if (s[i] == '#') {
if (i == 0) {
s = s.substr(1, s.size() - 1);
i -= 1;
size -= 1;
continue;
}
s = s.substr(0, i-1) + s.substr(i+1, s.size() - (i+1));
i -= 2;
size -= 2;
}
}
size = t.size();
for (int i = 0; i < t.size(); ++i) {
if (t.empty()) break;
if (t[i] == '#') {
if (i == 0) {
t = t.substr(1, t.size() - 1);
i -= 1;
size -= 1;
continue;
}
t = t.substr(0, i-1) + t.substr(i+1, t.size() - (i+1));
i -= 2;
size -= 2;
}
}
return s == t;
}
};
int main() {
Solution sol;
cout << sol.backspaceCompare("gtc#uz#", "gtcm##uz#");
return 0;
}