Friday 6 July 2012

Write a method to decide if two strings are premutation of each other or not.


#include<stdio.h>
#include<string.h>
int check_permutation(char s1[], char s2[])
{
    int c[27],i;
    for(i=0;i<27;i++)
        c[i]=0;
    if(strlen(s1)!=strlen(s2))
        return 0;
    for(i=0;s1[i]!=NULL;i++)
    {
        c[0]= s1[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]++;

        c[0]= s2[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]--;
    }
    for(i=1;i<27;i++)
    {
        if(c[i]!=0)
            return 0;
    }
    return 1;
}

int main()
{
    char s1[20],s2[20];
    scanf("%s%s",&s1,&s2);
    if( check_permutation(s1,s2) )
       printf("\n Permutation of each other");
    else
        printf("\n NOT a Permutation ");
return 0;
}

No comments:

Post a Comment