Skip to content

Commit ee46343

Browse files
committed
Day-9 removing duplicates from a string
1 parent 395cc64 commit ee46343

File tree

3 files changed

+314
-1
lines changed

3 files changed

+314
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This repository will contain random algorithm and data structure problems I resolve to solve at least one a day.
33

44
## Current Streak
5-
8 days, longest streak ( 8 days ) as of August 24, 2015.
5+
9 days, longest streak ( 9 days ) as of August 25, 2015.
66

77
## Include Directiory
88
Include directory and sub directories contain STL like header file implementation of various algorithms and data structures. Following header only implementation,
@@ -28,3 +28,4 @@ please let me know.
2828
- Problem 1 : Determine if a string has unique chars.
2929
- Problem 2 : (Edition 5) Reverse a c-string.
3030
- Problem 2 : (Edition 6) Determine if two string are permutations of each other.
31+
- Problem 3 : (Edition 5) Remove duplicate chars from a string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Cracking the coding interview edition *5* old one
3+
* 1.2 Reverse a string when you are a pass a null terminated C string.
4+
*/
5+
6+
7+
#include<iostream>
8+
#include<cstring>
9+
#include<generic.h>
10+
11+
void reverse1( char * str)
12+
{
13+
int len = std::strlen(str);
14+
for ( int i = 0; i < len/2; ++i ) {
15+
algo::swap( str[i], str[len - i - 1] );
16+
}
17+
}
18+
19+
void reverse2( char *str ) {
20+
if ( !str )
21+
return;
22+
char *p = str;
23+
char *q = str;
24+
while (*q) ++q;
25+
--q;
26+
//q now points to last char and p points to first char
27+
while ( p < q )
28+
std::swap( *p++, *q-- );
29+
}
30+
31+
int main()
32+
{
33+
char str[] = "abcdefghijklmnopqrstuvwxyz";
34+
reverse1(str);
35+
std::cout << "On reversing it once: " << str << std::endl;
36+
reverse2(str);
37+
std::cout << "On reversing it again: " << str << std::endl;
38+
return 0;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/*
2+
* Cracking the coding interview, edition 5
3+
* Problem 1.3
4+
* Write an algorithm to remove duplicate chars from a string
5+
*/
6+
7+
8+
#include <iostream>
9+
#include <cstring>
10+
11+
using std::string;
12+
using std::cin;
13+
using std::cout;
14+
using std::endl;
15+
16+
/*
17+
* Function - removeDuplicates1
18+
* Args - A string
19+
* Return - Arg string with no duplicate chars
20+
* Runtime - O(n)
21+
* Assumption - contains letter ( a - z )
22+
*/
23+
24+
string removeDuplicates1( const string & str )
25+
{
26+
int len = str.length();
27+
if ( len < 2 ) {
28+
return str;
29+
}
30+
int check = 0;
31+
string result("");
32+
for ( auto & ch : str ) {
33+
int c = (int)( ch - 'a');
34+
if ( (check & ( 1 << c )) == 0) {
35+
result += ch;
36+
check |= ( 1 << c);
37+
}
38+
}
39+
return result;
40+
}
41+
42+
/*
43+
* Function - removeDuplicates2
44+
* Args - A string
45+
* Return - Arg string with no duplicate chars
46+
* Runtime - O(n)
47+
* Assumption - ASCII extended char set
48+
*/
49+
50+
string removeDuplicates2 ( const string & str )
51+
{
52+
int len = str.length();
53+
if ( len < 2 )
54+
return str;
55+
string result("");
56+
bool check[256];
57+
for ( auto & ch : str ) {
58+
if ( !check[(int)ch] ) {
59+
check[(int)ch] = true;
60+
result += ch;
61+
}
62+
}
63+
return result;
64+
}
65+
66+
67+
68+
/*
69+
* Function - removeDuplicates3
70+
* Args - A string
71+
* Return - Arg string with no duplicate chars
72+
* Runtime - O(n^2)
73+
* Assumption - NONE
74+
*/
75+
76+
string removeDuplicates3( string str )
77+
{
78+
int len = str.length();
79+
if ( len < 2 ) {
80+
return str;
81+
}
82+
string result("");
83+
for ( int i = 0; i < len; ++i) {
84+
if ( str[i] != '\0' ) {
85+
result += str[i];
86+
for ( int j = i + 1; j < len; ++j ) {
87+
if ( str[i] == str[j] ) {
88+
str[j] = '\0';
89+
}
90+
}
91+
}
92+
}
93+
return result;
94+
}
95+
96+
97+
98+
99+
/*
100+
* Function - removeDuplicates4
101+
* Args - A C string
102+
* Return - void, we change the char array we pass as arg
103+
* Runtime - O(n)
104+
* Assumption - contains letter ( a - z )
105+
*/
106+
107+
void removeDuplicates4( char * str )
108+
{
109+
int len = std::strlen(str);
110+
if ( len < 2) {
111+
return;
112+
}
113+
int check = 0;
114+
int cindx = 0;
115+
for ( int i = 0; i < len; ++i) {
116+
int c = (int) ( str[i] - 'a');
117+
if ( (check & ( 1 << c )) == 0 ) {
118+
str[cindx++] = str[i];
119+
check |= ( 1 << c );
120+
}
121+
}
122+
str[cindx] = '\0';
123+
}
124+
125+
/*
126+
* Function - removeDuplicates5
127+
* Args - A c string
128+
* Return - void, we change the c-string we pass to function
129+
* Runtime - O(n^2)
130+
* Assumption - NONE
131+
*/
132+
133+
void removeDuplicates5( char * str )
134+
{
135+
int len = std::strlen(str);
136+
if ( len < 2 ) {
137+
return;
138+
}
139+
int idx = 0;
140+
for ( int i = 0; i < len; ++i) {
141+
if ( str[i] != '\0' ) {
142+
str[idx++] = str[i];
143+
for ( int j = i + 1; j < len; ++j ) {
144+
if ( str[i] == str[j] ) {
145+
str[j] = '\0';
146+
}
147+
}
148+
}
149+
}
150+
}
151+
152+
153+
/*
154+
* Function - removeDuplicates6
155+
* Args - A c string
156+
* Return - void, we change the c-string we pass to function
157+
* Runtime - O(n)
158+
* Assumption - ASCII extended char set
159+
*/
160+
161+
void removeDuplicates6 ( char *str )
162+
{
163+
int len = std::strlen(str);
164+
if ( len < 2 )
165+
return;
166+
bool check[256] = {false};
167+
int idx = 0;
168+
for ( int i = 0; i < len; ++i )
169+
{
170+
if ( !check[(int)str[i]] ) {
171+
check[(int)str[i]] = true;
172+
str[idx++] = str[i];
173+
}
174+
}
175+
str[idx] = '\0';
176+
}
177+
178+
int main()
179+
{
180+
string str1("aaaabbbbbcddeffgheee");
181+
string str2("abcd");
182+
string str3("a");
183+
string str4("");
184+
cout << "For method 1:" << endl;
185+
cout << "original-string:" << str1 << " after removing duplicates:" << removeDuplicates1(str1) << endl;
186+
cout << "original-string:" << str2 << " after removing duplicates:" << removeDuplicates1(str2) << endl;
187+
cout << "original-string:" << str3 << " after removing duplicates:" << removeDuplicates1(str3) << endl;
188+
cout << "original-string:" << str4 << " after removing duplicates:" << removeDuplicates1(str4) << endl;
189+
190+
191+
cout << "\n\nFor method 2:" << endl;
192+
cout << "original-string:" << str1 << " after removing duplicates:" << removeDuplicates2(str1) << endl;
193+
cout << "original-string:" << str2 << " after removing duplicates:" << removeDuplicates2(str2) << endl;
194+
cout << "original-string:" << str3 << " after removing duplicates:" << removeDuplicates2(str3) << endl;
195+
cout << "original-string:" << str4 << " after removing duplicates:" << removeDuplicates2(str4) << endl;
196+
197+
cout << "\n\nFor method 3:" << endl;
198+
cout << "original-string:" << str1 << " after removing duplicates:" << removeDuplicates3(str1) << endl;
199+
cout << "original-string:" << str2 << " after removing duplicates:" << removeDuplicates3(str2) << endl;
200+
cout << "original-string:" << str3 << " after removing duplicates:" << removeDuplicates3(str3) << endl;
201+
cout << "original-string:" << str4 << " after removing duplicates:" << removeDuplicates3(str4) << endl;
202+
203+
204+
cout << "\n\nFor method 4:" << endl;
205+
206+
char str5[] = "aaaabbbbcccc";
207+
char str6[] = "abcde";
208+
char str7[] = "a";
209+
char str8[] = "";
210+
211+
cout << endl;
212+
cout << "original-string:" << str5 << " after removing duplicates:";
213+
removeDuplicates4(str5);
214+
cout << str5 << endl;
215+
cout << "original-string:" << str6 << " after removing duplicates:";
216+
removeDuplicates4(str6);
217+
cout << str6 << endl;
218+
cout << "original-string:" << str7 << " after removing duplicates:";
219+
removeDuplicates4(str7);
220+
cout << str7 << endl;
221+
cout << "original-string:" << str8 << " after removing duplicates:";
222+
removeDuplicates4(str8);
223+
cout << str8 << endl;
224+
225+
226+
cout << "\n\nFor method 5:" << endl;
227+
228+
char str9[] = "aaaabbbbccccdddeeefffaaabbb";
229+
char str10[] = "abcdef";
230+
char str11[] = "b";
231+
char str12[] = "";
232+
233+
cout << endl;
234+
cout << "original-string:" << str9 << " after removing duplicates:";
235+
removeDuplicates5(str9);
236+
cout << str9 << endl;
237+
cout << "original-string:" << str10 << " after removing duplicates:";
238+
removeDuplicates5(str10);
239+
cout << str10 << endl;
240+
cout << "original-string:" << str11 << " after removing duplicates:";
241+
removeDuplicates5(str11);
242+
cout << str11 << endl;
243+
cout << "original-string:" << str12 << " after removing duplicates:";
244+
removeDuplicates5(str12);
245+
cout << str12 << endl;
246+
247+
cout << "\n\nFor method 6:" << endl;
248+
249+
char str13[] = "aaaabbbbccccdddeeefffaaabbb";
250+
char str14[] = "abcdef";
251+
char str15[] = "b";
252+
char str16[] = "";
253+
254+
cout << endl;
255+
cout << "original-string:" << str13 << " after removing duplicates:";
256+
removeDuplicates6(str13);
257+
cout << str13 << endl;
258+
cout << "original-string:" << str14 << " after removing duplicates:";
259+
removeDuplicates6(str14);
260+
cout << str14 << endl;
261+
cout << "original-string:" << str15 << " after removing duplicates:";
262+
removeDuplicates6(str15);
263+
cout << str15 << endl;
264+
cout << "original-string:" << str16 << " after removing duplicates:";
265+
removeDuplicates6(str16);
266+
cout << str16 << endl;
267+
268+
269+
270+
271+
cout << endl;
272+
return 0;
273+
}

0 commit comments

Comments
 (0)