-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackspaceStringCompare.cpp
More file actions
42 lines (35 loc) · 955 Bytes
/
backspaceStringCompare.cpp
File metadata and controls
42 lines (35 loc) · 955 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
40
41
42
class Solution {
public:
bool backspaceCompare(string s, string t) {
stack<char> s_stack, t_stack;
string s_final, t_final;
for(int i=0; i<s.length(); i++) {
if(s[i]=='#' && !(s_stack.empty())) {
s_stack.pop();
}
else if(s[i]!='#'){
s_stack.push(s[i]);
}
}
for(int i=0; i<t.length(); i++) {
if(t[i]=='#' && !(t_stack.empty())) {
t_stack.pop();
}
else if(t[i]!='#')
t_stack.push(t[i]);
}
while(!t_stack.empty()){
t_final += t_stack.top();
t_stack.pop();
}
while(!s_stack.empty()){
s_final += s_stack.top();
s_stack.pop();
}
cout<<s_final<<endl;
cout<<t_final<<endl;
if(s_final == t_final)
return true;
return false;
}
};