Joy And Two Strings

QUESTION

Joy has two strings S1 and S2 both consisting of lower case alphabets. He listed all subsequences of string S1 on a paper and all subsequences of string S2 on a separate paper. He wants to know whether there exists a string which is listed on both the papers.\n\nHelp Joy to accomplish this task.\n\nInput\n\nFirst line of input contains a single integer T denoting the number of test cases. Each test case consists of two lines. \n\nFirst line of each test case contains a string denoting string S1. Next line of each test case contains a string denoting string S2.\n\nOutput\n\nFor each test case, Print Yes if both papers contain a common string otherwise Print No.\n\nConstraints\n\n1 <= T <= 10^5\n\n1 <= |S1| <= 10^5\n\n1 <= |S2| <= 10^5\n\nSum of |S1| over all test case does not exceed 5*10^5\n\nSum of |S2| over all test case does not exceed 5*10^5\n\nSubtasks\n\nSubtask1 : sum of |S1|,|S2| over all test cases does not exceed 10^3 (25 pts)\nSubtask2 : sum of |S1|,|S2| over all test cases does not exceed 10^4 (25 pts)\nSubtask3 : sum of |S1|,|S2| over all test cases does not exceed 5*10^5 (50 pts).

ANSWER

#include <bits/stdc++.h>
#define ll long long
#define MAXN 100010
#define INF 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define f first
#define s second
using namespace std;
 
int T;
string a, b;
bool flag, letters[26];
 
int main () {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while (T--) {
        flag = 0;
        cin >> a >> b;
        memset(letters, 0, sizeof(letters));
        for (int i=0; i<a.size(); i++) letters[a[i] - 'a'] = 1;
        for (int i=0; i<b.size(); i++) {
          if (letters[b[i] - 'a']) {
            cout << "Yes" << endl;
            flag = 1;
            break;
          }
        }
        if (!flag) cout << "No" << endl;
    }
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.