Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Xsquare And Two Strings problem solution

YASH PAL, 31 July 2024
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("%lldn",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;
}
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programmingoneonone | WordPress Theme by SuperbThemes