Given strings S and T. The duty is to verify if S might be transformed to T by acting at most Ok operations. For the ith operation, choose any character in S which has not been chosen earlier than, and increment the chosen character i instances (i.e., changing it with the letter i instances forward within the alphabet)
Be aware: The increment is cyclic (i.e., incrementing ‘z’ by 1 makes the character ‘a’)
Enter: A = “enter”, B = “ouput”, N = 9
Rationalization: Within the sixth operation, we shift ‘i’ 6 instances to get ‘o’. And within the seventh operation, we shift ‘n’ to get ‘u’.
Enter: A = “aab”, B = “bbb”, N = 27
Rationalization: Within the 1st transfer, we shift the primary ‘a’ 1 time to get ‘b’. Within the twenty seventh transfer, we shift the second ‘a’ 27 instances to get ‘b’.
An method utilizing Hashing:
One necessary factor is to note that we are able to solely shift a letter as soon as, and we can not change multiple letter by the identical variety of shifts (i). In different phrases, if we shift one letter by 1, no different letters might be shifted by 1. If we have to shift by 1 once more, you could use “wrapping” and shift by 27 (which is 1 + 26).
Comply with the steps under to implement the above concept:
- Test if the dimensions of each strings shouldn’t be equal
- Initialize an array arr of dimension 26. the place, arr[i] = x implies that there are x characters in string A that want an increment of i instances to match the characters in string B.
- Iterate over the string A.
- Calculate the increment wanted to transform A[i] to B[i]
- Calculate the whole operation require to shift all of the characters that want the identical quantity of increment.
- If the whole operation required is larger than N, return false
- Increment the frequency of shift in array arr by 1
- Lastly, return true
Beneath is the implementation of the above method:
Time Complexity: O(dimension(A))
Auxiliary Area: O(1)