Localized search engine

QUESTION

Xsquare loves to play with strings a lot. Today, he has two strings S1 and S2 both consisting of lower case alphabets. \n\nXsquare listed all subsequences of string S1 on a paper and all subsequences of string S2 on a separate paper. \n\nXsquare wants to know whether there exists a string which is listed on both the papers.\n\nXsquare thinks that this task is pretty boring and handed it to you. Please accomplish this task on his behalf.\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. \nNext 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 <iostream>
 
int main()
{
	int t; 
	bool letters[26],common;
	std::string s1,s2;
	std::cin>>t;
	for(int i=0;i<t;i++) {
		std::cin>>s1>>s2;
		common=false;
		for(int j=0;j<26;j++) {
			letters[j]=false;
		} 
		for(int j=0;j<s1.size();j++) {
			letters[s1[j]-'a']=true;
		}
		for(int j=0;j<s2.size();j++) {
			if(letters[s2[j]-'a']) {
       //       std::cout<<"\n";
				std::cout<<"Yes"<<std::endl;
          //    std::cout<<"\n";
				common = true;
				break;
			}
		}
		if(!common) {
			std::cout<<"No"<<std::endl;
		}
	} 
}
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.

Powered By
CHP Adblock Detector Plugin | Codehelppro