-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTwo Strings.cpp
85 lines (59 loc) · 1.82 KB
/
Two Strings.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Hacker Rank */
/* Title - Two Strings */
/* Created By - Akash Modak */
/* Date - 04/09/2020 */
// Given two strings, determine if they share a common substring. A substring may be as small as one character.
// For example, the words "a", "and", "art" share the common substring . The words "be" and "cat" do not share a substring.
// Function Description
// Complete the function twoStrings in the editor below. It should return a string, either YES or NO based on whether the strings share a common substring.
// twoStrings has the following parameter(s):
// s1, s2: two strings to analyze .
// Input Format
// The first line contains a single integer , the number of test cases.
// The following pairs of lines are as follows:
// The first line contains string .
// The second line contains string .
// Constraints
// and consist of characters in the range ascii[a-z].
// Output Format
// For each pair of strings, return YES or NO.
// Sample Input
// 2
// hello
// world
// hi
// world
// Sample Output
// YES
// NO
#include <bits/stdc++.h>
using namespace std;
// Complete the twoStrings function below.
string twoStrings(string s1, string s2) {
unordered_map<char,int> m;
for(int i=0;i<s1.length();i++){
m[s1[i]]++;
}
for(int i=0;i<s2.length();i++){
if(m.count(s2[i])>0)
return "YES";
}
return "NO";
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int q_itr = 0; q_itr < q; q_itr++) {
string s1;
getline(cin, s1);
string s2;
getline(cin, s2);
string result = twoStrings(s1, s2);
fout << result << "\n";
}
fout.close();
return 0;
}