0

The question states: Two strings A and B comprising of lower case English letters are compatible if they are equal or can be made equal by following this step any number of times:

Select a prefix from the string A(possibly empty), and increase the alphabetical value of all the characters in the prefix by the same valid amount. For example, if the string is xyz and we select the prefix xy then we can convert it to yz by increasing the alphabetical value by 1. But if we select the prefix xyz then we cannot increase the alphabetical value. Your task is to determine if given strings A and B are compatible.

The logic here is that the difference of lower indices of the two string should never increase that of higher indices. That is what I did. And the two test cases are really long but not longer than what we can store in string type. That is why I have not provided it here.

#include<bits/stdc++.h>
using namespace std;

int main(){

ios_base::sync_with_stdio(false);
cin.tie(0);

string s1;
string s2;
cin>>s1>>s2;
long long int count=0,max=0;
if(s2.length()!=s1.length())
{
 cout<<"NO";
 return 0;
}
for(int i=0;s1[i]!='\0';i++)
{
 if(s1[i]==s2[i])
     break;
 else
     count++;
}
for(int i=count-1;i>=0;i--)
{
   if(s2[i]-s1[i]>=max && s2[i]-s1[i]>0 && s2[i]-s1[i]<26)
     max= s2[i]-s1[i];

   else
   {
      cout<<"NO";
      return 0;
   }
 }
 cout<<"YES";

 return 0;
}
//The tester correct code is as follows:

#include<bits/stdc++.h>
using namespace std;
int main()
{
 string a,b;
 assert(cin>>a>>b);
 assert(a.size()>=1 && a.size()<=1e6);
 assert(b.size()>=1 && b.size()<=1e6);
 bool f=1;
 if(a.size()!=b.size())f=0;
 else
 {
    int val=30;
    for(int i=0;i<a.size();i++)
    {
        assert(a[i]>='a' && a[i]<='z');
        assert(b[i]>='a' && b[i]<='z');
        int temp=b[i]-a[i];
        if(temp>val || temp<0){f=0;break;}
        val=temp;
    }
 }
 if(f)cout<<"YES\n";
 else cout<<"NO\n";
 return 0;
}
era s'q
  • 537
  • 1
  • 7
  • 27
  • 1
    Unrelated to your problem, but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jun 13 '19 at 09:24
  • 3
    As for your problem, do you have access to the input data for the failing tests? Then you could just [debug your program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) with that data, to find out what the problem is. – Some programmer dude Jun 13 '19 at 09:26
  • Can you give the source of this question? – Gaurav Singh Jun 13 '19 at 10:16
  • @GauravSingh [link](https://www.hackerearth.com/practice/algorithms/string-algorithm/basics-of-string-manipulation/practice-problems/algorithm/aliceandstrings-9da62aa7/) – era s'q Jun 13 '19 at 15:30
  • @Someprogrammerdude The test files are really long. I'm unable to copy it at once in my compiler. You can check the question I Have provided a link in the previous comment. – era s'q Jun 13 '19 at 15:32

0 Answers0