Check if string S can be converted to T by incrementing characters


In this problem, we will check whether it is possible to convert string S to T by incrementing the characters of S only once according to the given condition.

Here, we can increment any characters by 'I' only once. So, If we need to increment any other character by 'I' times, the value of K should be greater than 26 + I.

Problem statement – We have given a string S, T, and positive integer K. We need to convert the string S to T by following the rules below.

  • We can take any string character and increment it only once.

  • We can take any I, where 1 <= I <= K only once to increment a particular character by 'I'.

  • The increment is cyclic. So, 'z' becomes 'a'.

Sample examples

Input

S = "abcde", T = "bdgik"; K = 7;

Output

Yes

Explanation

  • For converting 'a' to 'b', we need 1 increment.

  • For converting 'b' to 'd', we need 2 increments.

  • For converting 'c' to 'g', we need 4 increments.

  • To convert 'd' to 'I', we need 5 increments.

  • To convert 'e' to 'k', we need 6 increments.

Here, all increment numbers are used only once and less than K. So, It is possible to convert string S to T.

Input

S = "pqr", T = "qrs"; K = 1;

Output

No

Explanation

  • We can increment 'p' by 1 to convert to 'q'.

  • Again, we need 1 increment to convert 'q' to 'r', but we already used 1 increment. So, we need 1 + 26 = 27 increments to convert 'q' to 'r'. Here, 27 is greater than K, so it is not possible to convert string S to T.

Input

S = "pqr", T = "qrs"; K = 56;

Output

Yes

Explanation

  • 'p' -> 'q' == 1 increment.

  • 'q' -> 'r' == 1 + 26 = 27 increments.

  • 'r' -> 's' == 1 + 26 + 26 = 53 increments.

All increments are less than K. So, we can convert string S to T.

Approach 1

This approach will take the circular difference between two characters at the same index of string S and T. We can count the number of characters with the same difference. The K should be greater than (difference + number * 26) to convert string S to T, as we can perform increment 'I' only once.

Algorithm

Step 1 – If the size of both strings is different, return false.

Step 2 – Initialize the 'freq' list to store the frequency of the difference.

Step 3 – Start traversing the string.

Step 4 – Take the circular difference between two characters of the string. For taking the circular difference, add 26 to the difference and take its modulo with 26.

Step 5 – The characters are the same if the difference is 0. If the difference is not 0, and diff + freq[diff] * 26 > K is true, return false.

Step 6 – Increment the frequency of difference in the list.

Step 7 – Return true if the loop completes all iterations.

Example

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

bool CanSToT(string S, string T, int K) {
   // Compare sizes
   if (S.size() != T.size())
      return false;
   // To store the frequency of characters
   vector<int> freq(26, 0);
   // Traverse string S
   for (int p = 0; p < S.size(); p++) {
      // Calculating the  required increment
      int diff = (T[p] - S[p] + 26) % 26;
      // To increment freq[diff] characters, we need minimum diff + freq[diff]*26 operations
      if (diff != 0 && diff + freq[diff] * 26 > K)
         return false;
      // Update character frequency
      freq[diff]++;
   }
   // Final answer
   return true;
}
int main() {
   string S = "abcde", T = "bdgik";
   int K = 7;
   if (CanSToT(S, T, K))
      cout << "Yes, it is possible to convert S to T.";
   else
      cout << "No, it is not possible to convert S to T.";
   return 0;
}

Output

Yes, it is possible to convert S to T.

Time complexity – O(N), where N is a string size.

Space complexity – O(1), as we use the list of constant sizes to store the frequency of difference.

We learned to convert string S to T by performing the unique increments. Programmers also use the map to store the frequency of the difference.

Updated on: 29-Aug-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements