-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two_Strings.c
66 lines (57 loc) · 1.61 KB
/
Two_Strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Problem Statement
You are given two strings, A and B. Find if there is a substring that appears in both A and B.
Input Format
Several test cases will be given to you in a single file.
The first line of the input will contain a single integer T, the number of test cases.
Then there will be T descriptions of the test cases. Each description contains two lines.
The first line contains the string A and the second line contains the string B.
Output Format
For each test case, display YES (in a newline), if there is a common substring. Otherwise, display NO.
Constraints
All the strings contain only lowercase Latin letters.
1<=T<=10
1<=|A|,|B|<=105
/*
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int a[26] = {0,}; //hash value container
int is_substring(char s1[],char s2[]){
int i = 0;
int len = 0;
len = strlen(s1);
for(i=0;i< len;i++){
a[s1[i] -'a']++;
}
len = strlen(s2);
for(i=0;i< len;i++){
if(a[s2[i] -'a']){
return 1;
}
}
return 0;
}
int main() {
int t = 0; //no of test cases
char s1[100001] = {'\0',};
char s2[100001] = {'\0',};
int i = 0;
int len = 0; // length of the strings
scanf("%d",&t);
for(;t--;){
scanf("%s",s1);
scanf("%s",s2);
if(is_substring(s1,s2)){
printf("YES\n");
}
else{
printf("NO\n");
}
memset(s1,'\0',sizeof(s1));
memset(s1,'\0',sizeof(s1));
memset(a,0,sizeof(a));
}
return 0;
}