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 (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 and we select the prefix then we can convert it to by increasing the alphabetical value by 1. But if we select the prefix then we cannot increase the alphabetical value.
Your task is to determine if given strings and are compatible.
Example:
Input: A = "abaca", B = "cdbda"
Output: NO
Approach
C++
#include <bits/stdc++.h>using namespace std;void aliceStrings(string A, string 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 = i; j >= 0; j--){if ((B[j] - A[j]) != p){flag = 1;break;}}if (flag == 0)cout << "YES\n";elsecout << "NO\n";}}int main(){string A = "abaca", B = "cdbda";aliceStrings(A, B);return 0;}
No comments:
Post a Comment