-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackspace.cpp
More file actions
40 lines (33 loc) · 770 Bytes
/
backspace.cpp
File metadata and controls
40 lines (33 loc) · 770 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
37
38
39
#include<bits/stdc++.h>
using namespace std;
bool backspaceCompare(string s, string t) {
stack<char>stack_s;
stack<char>stack_t;
for(char c : s){
if(c != '#'){
stack_s.push(c);
}else{
if(!stack_s.empty())stack_s.pop();
}
}
for(char c : t){
if(c != '#'){
stack_t.push(c);
}else{
if(!stack_t.empty())stack_t.pop();
}
}
while(!stack_s.empty() && !stack_t.empty()){
if(stack_s.top() == stack_t.top()){
stack_s.pop();
stack_t.pop();
}else{
return false;
}
}
return (stack_s.empty() && stack_t.empty());
}
int main(){
cout << backspaceCompare("ab","ac#b");
return 0;
}