Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Given strings S and T. The duty is to verify if S might be transformed to T by acting at most Okay operations. For the ith operation, choose any character in S which has not been chosen earlier than, and increment the chosen character i occasions (i.e., changing it with the letter i occasions forward within the alphabet)
Be aware: The increment is cyclic (i.e., incrementing ‘z’ by 1 makes the character ‘a’)
Examples:
Enter: A = “enter”, B = “ouput”, N = 9
Output: True
Rationalization: Within the sixth operation, we shift ‘i’ 6 occasions to get ‘o’. And within the seventh operation, we shift ‘n’ to get ‘u’.Enter: A = “aab”, B = “bbb”, N = 27
Output: True
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 occasions to get ‘b’.
One necessary factor is to note that we are able to solely shift a letter as soon as, and we can not change a couple of 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 might want to use “wrapping” and shift by 27 (which is 1 + 26).
Comply with the steps under to implement the above thought:
Under is the implementation of the above strategy:
|
Time Complexity: O(measurement(A))
Auxiliary House: O(1)
[ad_2]