voidremoveDuplicates(char*str)
{
if (!str) {
return;
}
int len =strlen(str);
if (len <2){
return;
}
int tail =1;
for (int i =1; i < len; ++i) {
int j;
for (j =0; j < tail; ++j)
if (str[i] == str[j]) {
break;
}
if (j == tail) {
str[tail] = str[i];
++tail;
}
}
str[tail] ='\0';
}
Similar to approach with hashtable, but if chars are between a-z, or ascii, just use array
Here is the procedure
Convert the string to character array.
Create the boolean array of 256,( for all 0 – 255 ASCII characters)
Iterate through character array from step one and set the Boolean array index ( index = ascii value of the character). Ignore if that index is already true.
Create a separate array based on the index set as true in Boolean array.
This approach will retain the order of characters.
voidremoveDuplicates(char* str) {
if (!str)
return;
int len =strlen(str);
if (len <2)
return;
bool hit[256];
for (int i =0; i <256; ++i)
hit[i] = false;
hit[str[0]] = true;
int tail =1;
for (int i =1; i < len; ++i) {
if (!hit[str[i]]) {
str[tail] = str[i];
++tail;
hit[str[i]] = true;
}
}
str[tail] ='\0';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
publicstatic String removeDuplicates(String s) {
char[] chrArr = s.toCharArray();
boolean[] asciiChrSet =newboolean[256];
StringBuilder stb =new StringBuilder();
for (int i = 0; i < chrArr.length; i++) {
if (asciiChrSet[chrArr[i]]) {
continue;
}
asciiChrSet[chrArr[i]]=true;
stb.append(chrArr[i]);
}
return stb.toString();
}
Putting the constraint of space and ordering
Suppose the problem becomes:
Write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.
Then what solution do we have? Method 2 cannot be used as we want to keep the ordering same.
Method 3 cannot be used, as it uses hashtable, which uses extra spaces.
Well, nothing is stopping us from using method 1, but it is brute force, and can we do better?
The answer is yes, if the interviewer confirms, that your string is a constrained by say it can have only small case integers.
Since one or two additional variables are fine but no buffer is allowed, you can simulate the behaviour of a hashmap by using an integer to store bits instead.
This does not work for normal cases. It will only work from a..z case sensitive. No spaces supported. This would work miracles in a 256bit system to process the entire ASCII range. But it does run in O(N). I still +1 this one.
publicstaticvoidremoveDuplicates(char[] str) {
int map = 0;
for (int i = 0; i < str.length; i++) {
if ((map & (1 << (str[i]-'a'))) > 0) // duplicate detected str[i]= 0;
else// add unique char as a bit '1' to the map map |= 1 << (str[i]-'a');
}
}