-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackspace_string_compare.cpp
49 lines (47 loc) · 1.2 KB
/
backspace_string_compare.cpp
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
48
49
//program to check if two string are same or not after removing from some charecter with backspace
//problem link: https://leetcode.com/problems/backspace-string-compare
class Solution {
public:
bool backspaceCompare(string s, string t) {
stack<char>a,b;
for(int i = 0; i < s.size(); i++)
{
if(!a.empty()&&s[i]=='#')
{
a.pop();
}
else if(s[i]!='#')
{
a.push(s[i]);
}
}
for(int i = 0; i< t.size(); i++)
{
if(!b.empty()&&t[i]=='#')
{
b.pop();
}
else if(t[i]!='#')
{
b.push(t[i]);
}
}
cout<<"a size :"<<a.size()<<" b size is :"<<b.size()<<endl;
if(a.size()!=b.size())
{
return false;
}
else
{
while(!a.empty())
{
cout<<a.top()<<" "<<b.top()<<endl;
if(a.top()!=b.top())return false;
a.pop();
b.pop();
}
return true;
}
return true;//this line will not exicute.
}
};