https://mycode.prepbytes.com/problems/strings/ALIENFRIEND
My approach :-
-
find total ‘?’ in left side and store it in left
-
find total ‘?’ in right side and store it in right
find leftside as left^2;
find rightside as right^2;
-
if any two characters(except if any one of them is ‘?’) are not equal
then ans is leftside*rightside
-
if(leftside==1) ans is (rightside-1)
-
if(rightside==1) ans is (leftside-1)
6)else ans is (leftside)*(rightside-1)
My code for this problem :-
#include <bits/stdc++.h>
using namespace std;
int main()
{
//write your code here
int t; cin>>t;
for(int i=0;i<t;i++)
{
string str; cin>>str;
int flag=0; int size=str.length();
int left=0,right=0;
int z=0;
for(int g=0;g<str.length()/2;g++)
{
//cout<<"indexes are "<<g<<" "<<(size/2+g)<<endl;
//cout<<str[g]<<" "<<str[size/2+g]<<endl;
if((str[g]!='?')&&(str[size/2+g]!='?')&&(str[g]!=str[size/2+g]))
{flag=1; }
if(str[g]=='?')
left++;
if(str[size/2+g]=='?')
right++;
z++;
}
// cout<<"left and right is "<<left<<" "<<right<<endl;
int leftsum=1;
while(left--)
{
leftsum=(leftsum*2)%1000000007;
}
int rightsum=1;
while(right--)
{
rightsum=(rightsum*2)%1000000007;
}
//cout<<"leftsum and rightsum is "<<leftsum<<" "<<rightsum<<endl;
//cout<<"flag is "<<flag<<endl;
if(flag==1)
{
cout<<(leftsum*rightsum)%1000000007;
}
else if(leftsum==1)
{
cout<<rightsum-1;
}
else if(rightsum==1)
{
cout<<leftsum-1;
}
else
{
cout<<((leftsum)*(rightsum-1))%1000000007;
}
cout<<endl;
}
return 0;
}