Super Reduced String Problem Solution

HackeRank Super Reduced String Problem
Steve has a string, , consisting of  lowercase English alphabetic letters. In one operation, he can delete any pair of adjacent letters with same value. For example, string "aabcc" would become either "aab" or "bcc" after  operation.
Steve wants to reduce  as much as possible. To do this, he will repeat the above operation as many times as it can be performed. Help Steve out by finding and printing 's non-reducible form!
Note: If the final string is empty, print Empty String .

----------------------------------------In This Solution Solved 16/16 Test Cases-------------------------
#include <stdio.h>
#include <string.h>
#include <math.h>
/**
 * Created by abhimanoj on 09/2/16.
 */
char str[101];
void removeCharacter(int i)
{
    for(;str[i]!='\0';)
    {
        str[i]=str[i+2];
        str[i+1]=str[i+3];
        i+=2;

    }
}

int main()
{
    int i=0,len=0;
    scanf("%s",str);
    for(;str[len]!='\0';len++);
    for(;i<len;)
    {
        if((str[i] == str[i+1] ) && (i >= 0) && str[i]!='\0')
        {
            removeCharacter(i);
            i--;
        }
        else
        {
            i++;
        }
    }
    if(str[0]=='\0')
       printf("Empty String");
    else
       printf("%s\n",str);

    return 0;
}


-----------------------------------In This Solution Only Solved 11/16 Test Cases-------------------------------
#include <stdio.h>
#include <string.h>
#include <math.h>
/**
 * Created by abhimanoj on 09/2/16.
 */
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
 
    char str[101];
    int countStr[26]={0};
    int i,k=0;
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++){
        if(str[i]==str[i+1]){
        countStr[str[i]-97]+=1;
        i=0;
        }
    }
    for(i=0;i<26;i++){
        if(!(countStr[i]%2==0)){
            printf("%c",97+i);
            k++;
        }        
    }
    if(k==0)
        printf("Empty String");
    return 0;
}

Comments

Popular posts from this blog