Two Strings : HackerRank Problem Solution


You are given two strings, A and B. Find if there is a sub string that appears in both A and B.

Input:

2

hello
world
hi
world

Output:


YES

NO

Implementation:


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 1000000

int main() {

    char *s,*v;
    int t;
    int i;
    int flag;
    int hashs[26],hashv[26];
    scanf("%d",&t);
    s=(char*)malloc(sizeof(char)*MAX);
    v=(char*)malloc(sizeof(char)*MAX);
    while(t--)
        {
        flag=0;
        scanf("%s",s);
        scanf("%s",v);
        memset(hashs,0,sizeof(hashs));
        memset(hashv,0,sizeof(hashv));
        i=0;
        while(*(s+i))
            {
            hashs[*(s+i)-'a']++;
            i++;
        }
        i=0;
        while(*(v+i))
            {
            hashv[*(v+i)-'a']++;
            i++;
        }
        i=0;
        while(i<26)
            {
            if(hashs[i]!=0 && hashv[i]!=0)
                {
                flag=1;
                break;
            }
            i++;
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}