mfalko
BAN USERThis is what I was able to come up with. I wanted to keep it as simple as I could. the following code uses two passes, the first pass creates a histogram of character counts. The second pass uses two pointers to walk the string and, using the information in the histogram, decides which characters to keep and which characters to remove.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
if (argc < 2) {
return EXIT_FAILURE;
}
int hist[26] = {0};
char* forwardProbe = argv[1];
char* rearProbe = forwardProbe;
while (*forwardProbe) {
hist[*forwardProbe - 'a']++;
forwardProbe++;
}
forwardProbe = rearProbe;
while (*forwardProbe) {
switch (hist[*forwardProbe - 'a']) {
case 0:
break;
case 1:
*rearProbe = *forwardProbe;
rearProbe++;
hist[*forwardProbe - 'a'] = 0;
break;
default:
if (*forwardProbe < *(forwardProbe + 1)
|| (rearProbe == argv[1]? *rearProbe < *forwardProbe
: *(rearProbe -1) < *forwardProbe) )
{
*rearProbe = *forwardProbe;
rearProbe++;
hist[*forwardProbe - 'a'] = 0;
} else {
hist[*forwardProbe - 'a']--;
}
break;
}
forwardProbe++;
}
*rearProbe = '\0';
printf("%s\n", argv[1]);
return EXIT_SUCCESS;
}
I just realized I posted this twice, I thought the site glitched when I first posted, then I logged in and posted again. Can this be deleted?
- mfalko September 09, 2015