-
Notifications
You must be signed in to change notification settings - Fork 0
/
backspace_string_com.cpp
90 lines (65 loc) · 2.02 KB
/
backspace_string_com.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Solution {
public:
bool backspaceCompare(string s, string t) {
// s="ab##";
// t="c#d#";
// int slength = s.length();
// int tlength = t.length();
// int i = 0;
// int j = 0;
// while( i < slength){
// if(s[i] == '#'){
// s.erase(i-1, 2);
// slength= s.length();
// i= i-2;
// }else{
// i++;
// }
// }
// while( j < tlength){
// if(t[i] == '#'){
// t.erase(j-1, 2);
// tlength= t.length();
// j= j-2;
// }else{
// j++;
// }
// }
// s= "xywrrmp";
// t="xywrrmu#p";
for(int i=0; i < s.length(); i++){
if(s[i] == '#'){
s[i] = '1';
int temp = i-1;
while(temp>= 0){
if(s[temp] != '1'){
s[temp] = '1';
break;
}
temp--;
}
}
}
for(int j=0; j < t.length(); j++){
if(t[j] == '#'){
t[j] = '1';
int temp = j-1;
while(temp>= 0){
if(t[temp] != '1'){
t[temp] = '1';
break;
}
temp--;
}
}
}
s.erase(remove( s.begin(), s.end(), '1'), s.end());
t.erase(remove( t.begin(), t.end(), '1'), t.end());
cout<<"s is "<<s<<endl;
cout<<"t s "<<t<<endl;
if(s==t){
return true;
}
return false;
}
};