-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.cpp
More file actions
36 lines (31 loc) · 760 Bytes
/
Solution.cpp
File metadata and controls
36 lines (31 loc) · 760 Bytes
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
#include <string>
using namespace std;
class Solution {
public:
// go from back to front
bool backspaceCompare(string s1, string s2) {
int l1 = s1.size(), l2 = s2.size();
string r1 = "", r2 = "";
int scnt = 0;
for (int i = l1 - 1; i >= 0 ; i -- ) {
if (s1[i] == '#') scnt ++ ;
else if (scnt > 0) scnt -- ;
else {
r1 += s1[i];
}
}
scnt = 0;
for (int i = l2 - 1; i >= 0 ; i -- ) {
if (s2[i] == '#') scnt ++ ;
else if (scnt > 0) scnt -- ;
else {
r2 += s2[i];
}
}
return r1 == r2;
}
};
int main(int argc, char const *argv[])
{
return 0;
}