forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
1.3.cpp
117 lines (112 loc) · 2.42 KB
/
1.3.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <cstring>
using namespace std;
string removeDuplicate1(string s)
{
int check = 0;
int len = s.length();
if(len < 2) return s;
string str = "";
for(int i=0; i<len; ++i)
{
int v = (int)(s[i]-'a');
if((check & (1<<v)) == 0)
{
str += s[i];
check |= (1<<v);
}
}
return str;
}
string removeDuplicate2(string s)
{
int len = s.length();
if(len < 2) return s;
string str = "";
for(int i=0; i<len; ++i)
{
if(s[i] != '\0')
{
str += s[i];
for(int j=i+1; j<len; ++j)
if(s[j]==s[i])
s[j] = '\0';
}
}
return str;
}
void removeDuplicate3(char s[])
{
int len = strlen(s);
if(len < 2) return;
int p = 0;
for(int i=0; i<len; ++i)
{
if(s[i] != '\0')
{
s[p++] = s[i];
for(int j=i+1; j<len; ++j)
if(s[j]==s[i])
s[j] = '\0';
}
}
s[p] = '\0';
}
void removeDuplicate4(char s[])
{
int len = strlen(s);
if(len < 2) return;
bool c[256];
memset(c, 0, sizeof(c));
int p = 0;
for(int i=0; i<len; ++i)
{
if(!c[s[i]])
{
s[p++] = s[i];
c[s[i]] = true;
}
}
s[p] = '\0';
}
void removeDuplicate5(char s[])
{
int len = strlen(s);
if(len < 2) return;
int check = 0, p = 0;
for(int i=0; i<len; ++i)
{
int v = (int)(s[i]-'a');
if((check & (1<<v))==0)
{
s[p++] = s[i];
check |= (1<<v);
}
}
s[p] = '\0';
}
int main()
{
string s1 = "abcde";
string s2 = "aaabbb";
string s3 = "";
string s4 = "abababc";
string s5 = "ccccc";
cout<<removeDuplicate1(s1)<<" "<<removeDuplicate2(s1)<<endl;
cout<<removeDuplicate1(s2)<<" "<<removeDuplicate2(s2)<<endl;
cout<<removeDuplicate1(s3)<<" "<<removeDuplicate2(s3)<<endl;
cout<<removeDuplicate1(s4)<<" "<<removeDuplicate2(s4)<<endl;
cout<<removeDuplicate1(s5)<<" "<<removeDuplicate2(s5)<<endl;
char ss1[] = "abcde";
char ss2[] = "aaabbb";
char ss3[] = "";
char ss4[] = "abababc";
char ss5[] = "ccccc";
removeDuplicate5(ss1);
removeDuplicate5(ss2);
removeDuplicate5(ss3);
removeDuplicate5(ss4);
removeDuplicate5(ss5);
cout<<ss1<<" "<<ss2<<" "<<ss3<<" "<<ss4<<" "<<ss5<<endl;
return 0;
}