Skip to content

Commit 19b540e

Browse files
authored
Create 844.Backspace-String-Compare.cpp
1 parent c04fe08 commit 19b540e

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
bool backspaceCompare(string S, string T)
4+
{
5+
int i = squeeze(S);
6+
int j = squeeze(T);
7+
8+
return S.substr(i)==T.substr(j);
9+
}
10+
11+
int squeeze(string& S)
12+
{
13+
int i = S.size()-1;
14+
int j = S.size()-1;
15+
int count = 0;
16+
17+
while (i>=0)
18+
{
19+
while (count>0 || i>=0 && S[i]=='#')
20+
{
21+
if (S[i]=='#')
22+
count++;
23+
else
24+
count--;
25+
i--;
26+
}
27+
if (i>=0)
28+
{
29+
S[j] = S[i];
30+
j--;
31+
i--;
32+
}
33+
}
34+
return j+1;
35+
}
36+
};

0 commit comments

Comments
 (0)