In this challenge, first we want to find out the maximum value between number of occurrences for each characters, then only return characters which have the same number of occurrences as maximum value.

For example, "abcde" is a valid string, because max count of characters is 1, and other all character counts are one, so we can return "abcde". On the other hand, "abcdeee" is not valid, because count of 'e' is 3, not 1 as the others. So we need to get rid of "a","b","c" and "d", so we can return "eee".

Specification

string_editor(str)
Remove all characters from string according to maximum count of a character in the input String

Parameters
str: char* - input to work on

Return Value
char* - final decision about that string

Constraints
Input string consists of English alphabet characters
All of the characters are lowercase

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

#include <stdlib.h>
#include <string.h>

char* string_editor(const char* str)
{
	size_t s = 0;
	size_t count[256] = { 0 };
	size_t size = strlen(str);
	size_t max = 0;
	char *new = (char*)calloc(size + 1, sizeof(char));
	for (size_t i = 0; i < size; ++i)
		++count[str[i]];
	for (size_t i = 0; i < size; ++i)
		if (count[str[i]] > max)
			max = count[str[i]];
	for (size_t i = 0; i < size; ++i)
		if (count[str[i]] == max)
			new[s++] = str[i];
	new = (char *)realloc(new, (s + 1) * sizeof(char));
	new[s] = '\0';
	return new;
}

Examples

"cac" => returns "cc"

"aabcc" => returns "aacc"

"aaabbbg" => returns "aaabbb"

"pppenm" => returns "ppp"

"ekekua" => returns "ekek"

Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char* string_editor(const char* str)
    {
    	size_t s = 0;
    	size_t size = strlen(str);
    	char *new = (char*)calloc(size+1,sizeof(char));
    	for (size_t i = 0; i < size; i++)
    		for (size_t j = 0; j < size; j++)
    			if ((str[i] == str[j]) && (i != j))
    			{
    				new[s++] = str[i];
    				break;
    			}
    	new = (char *)realloc(new, (s + 1) * sizeof(char));
    	new[s] ='\0';
    	return new;
    }
  • 11
    #include <stdlib.h>
    
    2
    #include <stdio.h>
    
    33
    #include <string.h>
    
    44
    5
    char* string_editor(char* str)
    
    4+
    char* string_editor(const char* str)
    
    66
    {
    
    7
    	char temp = 0;
    
    8
    	char size = (char)strlen(str);
    
    9
    	char *new = (char*)calloc(size,sizeof(char));
    
    10
    	for (char i = 0; i < size; i++)
    
    11
    		for (char j = 0; j < size; j++)
    
    6+
    	size_t s = 0;
    
    7+
    	size_t size = strlen(str);
    
    8+
    	char *new = (char*)calloc(size+1,sizeof(char));
    
    9+
    	for (size_t i = 0; i < size; i++)
    
    10+
    		for (size_t j = 0; j < size; j++)
    
    1212
    			if ((str[i] == str[j]) && (i != j))
    
    13
    				new[temp++] = str[i];
    
    14
    	new[temp] ='\0';
    
    12+
    			{
    
    13+
    				new[s++] = str[i];
    
    14+
    				break;
    
    15+
    			}
    
    16+
    	new = (char *)realloc(new, (s + 1) * sizeof(char));
    
    17+
    	new[s] ='\0';
    
    1515
    	return new;
    
    1616
    }
    
Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char *string_editor(const char *str_in) {
    	  size_t lens = strlen(str_in), s = 0;
    	  char *str_out = (char *) calloc(lens + 1, sizeof(char));
    	  for(size_t i=0; i<lens; i++) {
            char curr = str_in[i];
    		    for(size_t j=0; j<lens; j++) {
    			      if(str_in[j] == curr && i != j) {
    				        str_out[s++] = curr;
                    break;
                }
            }
        }
        str_out = (char *) realloc(str_out, (s + 1) * sizeof(char));
        return str_out;
    }
  • 66
    	  char *str_out = (char *) calloc(lens + 1, sizeof(char));
    
    77
    	  for(size_t i=0; i<lens; i++) {
    
    88
            char curr = str_in[i];
    
    99
    		    for(size_t j=0; j<lens; j++) {
    
    1010
    			      if(str_in[j] == curr && i != j) {
    
    1111
    				        str_out[s++] = curr;
    
    12+
                    break;
    
    1212
                }
    
    1313
            }
    
    1414
        }
    
    1515
        str_out = (char *) realloc(str_out, (s + 1) * sizeof(char));
    
    1616
        return str_out;
    
    1717
    }
    

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* string_editor(char* str)
{
	char temp = 0;
	char size = (char)strlen(str);
	char *new = (char*)calloc(size,sizeof(char));
	for (char i = 0; i < size; i++)
		for (char j = 0; j < size; j++)
			if ((str[i] == str[j]) && (i != j))
				new[temp++] = str[i];
	new[temp] ='\0';
	return new;
}