Wednesday 22 June 2016

Given three strings A, B and C. Write a function that checks whether C is an interleaving of A and B. C is said to be interleaving A and B, if it contains all characters of A and B and order of all characters in individual strings is preserved.

#include <stdio.h>

int main()
{
 
    char first[10],second[10],third[10];
    scanf("%s%s%s",&first,&second,&third);
 
    int count = 0, kount = 0, findex = -1, eindex = -1, flag = 0, chumma = 0;
 
    for(int i=0;i<strlen(first);i++)
    {
        for(int j=chumma;j<strlen(third);j++)
        {
            if(first[i] == third[j] && findex < j)
            {
                count++;
                findex = j;
                chumma = j;
                break;
            }
            else if (first[i] == third[j] && findex > j)
            {
                flag = 1;
                break;
            }
        if (flag == 1)
            break;
        }
    }
 
    flag = 0;
    chumma = 0;
    for(int i=0;i<strlen(second);i++)
    {
        for(int j=chumma;j<strlen(third);j++)
        {
            if(second[i] == third[j] && eindex < j)
            {
                kount++;
                eindex = j;
                chumma = j;
                break;
            }
            else if (second[i] == third[j] && eindex > j)
            {
                flag = 1;
                break;
            }
        if (flag == 1)
            break;
        }
    }
    if ( count == strlen(first) && kount == strlen(second) )
        printf("True");
    else
        printf("False");
     
    return 0;
}

No comments:

Post a Comment