Alice and Strings

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.

Example:

Input:  A = "abaca", B = "cdbda"
Output: NO

Approach

C++

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

void aliceStrings(string Astring B)
{
    if (A.size() != B.size())
        cout << "NO\n";
    else if (A == B)
        cout << "YES\n";
    else
    {
        int i = A.size() - 1;
        while (i > 0 && A[i] == B[i])
        {
            i--;
        }
        int p = B[i] - A[i];

        int flag = 0;
        for (int j = ij >= 0j--)
        {
            if ((B[j] - A[j]) != p)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
}
int main()
{
    string A = "abaca"B = "cdbda";

    aliceStrings(AB);

    return 0;
}


No comments:

Post a Comment