Header Ad

HackerEarth Xsquare And Two Strings problem solution

In this HackerEarth Xsquare And Two Strings, problem-solution Xsquare loves to play with strings a lot. Today, he has two strings S1 and S2 both consisting of lower case alphabets. Square listed all subsequences of string S1 on a paper and all subsequences of string S2 on a separate paper. Square wants to know whether there exists a string that is listed on both papers.

Square thinks that this task is pretty boring and handed it to you. Please accomplish this task on his behalf.


HackerEarth Xsquare And Two Strings problem solution


HackerEarth Xsquare And Two Strings problem solution.

#include <bits/stdc++.h>
using namespace std ;
#define LL long long int
#define ft first
#define sd second
#define PII pair<int,int>
#define MAXN 1000001
#define mp make_pair
#define f_in(st) freopen(st,"r",stdin)
#define f_out(st) freopen(st,"w",stdout)
#define sc(x) scanf("%d",&x)
#define scll(x) scanf("%lld",&x)
#define pr(x) printf("%lld\n",x)
#define pb push_back
#define MOD 1000000007

string s1,s2 ;
int T,M[26];

int main(){
sc(T) ;
while(T--){
cin >> s1 ;
cin >> s2 ;
set<char> S1(s1.begin(),s1.end());
set<char> S2(s2.begin(),s2.end());
memset(M,0,sizeof M) ;
while(!S1.empty()){
M[*S1.begin()-'a'] = 1 ;
S1.erase(S1.begin()) ;
}
bool ok = 0;
while(!S2.empty()){
if(M[*S2.begin()-'a'])
ok = 1 ;
S2.erase(S2.begin()) ;
}
puts( ok ? "Yes" : "No" ) ;
}
return 0 ;
}

Second solution

#include <bits/stdc++.h>

using namespace std;

int main()
{
int t,n1,n2,mask1,mask2;
string s1,s2;
cin >> t;
while ( t-- ) {
cin >> s1 >> s2;
n1 = (int)s1.size(), n2 = (int)s2.size();
mask1 = mask2 = 0;
for ( int i = 0; i < n1; i++ ) mask1 = mask1 | (1<<(s1[i]-'a'));
for ( int i = 0; i < n2; i++ ) mask2 = mask2 | (1<<(s2[i]-'a'));
for ( char p = 'a'; p <= 'z'; p++ ) {
if ( mask1 & (1<<(p-'a')) ) {
if ( mask2 & (1<<(p-'a')) ) {
cout << p << endl;
cout << "Yes" << endl;
goto p1;
}
}
}
cout << "No" << endl;
p1: { }
}
return 0;
}

Post a Comment

0 Comments