Yahoo coding problem1:
Given string contains abcabcc i.e contains only a,b,c replace missing character from non two repeating characters if characters repeating simply move forword
i.e
testcase1:abc->cc
testcase 2:abca->cca->cb->a
solution1:
#include<string.h>
int main()
{
char a[]="aabc";
int b[3]={0,0,0};
int i,j=0,k=1,m,l=0,flag=1;
while(a[j+1]!='\0' || k == 1 )
{
k=0;
getchar();
printf("%s\n",a);
printf(" out j = %d\n",j);
m=0;l=0;k=0;
for(j=0;j<strlen(a)&&m!=1;)
{
b[a[j]-'a']=1;
b[a[j+1]-'a']=1;
m=0;
if(a[j]!=a[j+1])
{
if(b[0]==0) l=0;
else if(b[1]==0) l=1;
else l=2;
a[j]=l+'a';
for(l=j+1;l<strlen(a);l++)
{
a[l]=a[l+1];
}
a[l]='\0';
b[0]=0;b[1]=0;b[2]=0;
m=1;
++j;
printf("j = %d\n",j);
//if(a[j+1] == '\0') flag=0;
}
else
{
j++;
if(a[j+1]=='\0')
m=1;
b[0]=0;b[1]=0;b[2]=0;
}
}//for loop
for(i=0;i<strlen(a)-1;i++)
if(a[i] != a[i+1])
k=1;
} //while
printf("%s",a);
}
solution2:
#include<stdio.h>
#include<string.h>
int hash[3]={0,};
int main()
{
char a[]="aabc",miss,b[10]="";
int i=0,k=0,j=0,t;
while(a[i]!='\0' && a[i+1]!='\0')
{
if(a[i]!=a[(i+1)])
{
hash[a[i]-'a']=1;
hash[a[i+1]-'a']=1;
for(i=0;i<3;i++)
{
if(hash[i]==0)
{
miss=i+'a';
}
}
i=k;
b[k++]=miss;
for(j=i+2;a[j]!='\0';j++)
{
b[k]=a[j];
k++;
}
strcpy(a,b);
hash[0]=0;hash[1]=0;hash[2]=0;
for(t=0;t<10;t++)
{
b[t]='\0';
}
i=0;
k=0;
j=0;
}
else
{
if(strlen(a)>2)
{
b[k]=a[i];
k++;
i++;
}
else
{
break;
}
}
//len=strlen(a);
//printf("%c--%c--i=%d--j=%d--k=%d",a[i],a[i+1],i,j,k);
}
printf("\n%s",a);
return 0;
}
No comments:
Post a Comment