304 North Cardinal St.
Dorchester Center, MA 02124

# Examine if string S could be transformed to T by incrementing characters

Given strings S and T. The duty is to test if S could 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 instances (i.e., changing it with the letter i instances forward within the alphabet)

Observe: 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 instances 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 instances to get ‘b’.

## An strategy utilizing Hashing:

One necessary factor is to note that we will solely shift a letter as soon as, and we can’t change multiple letter by the identical variety of shifts (i). In different phrases, if we shift one letter by 1, no different letters could be shifted by 1. If we have to shift by 1 once more, you have to use “wrapping” and shift by 27 (which is 1 + 26).

Comply with the steps beneath to implement the above thought:

• Examine if the scale of each strings will not 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 entire operation require to shift all of the characters that want the identical quantity of increment.
• If the entire operation required is bigger than N, return false
• Increment the frequency of shift in array arr by 1
• Lastly, return true

Under is the implementation of the above strategy:

## C++

 ` `  `#embody ` `utilizing` `namespace` `std;` ` `  `bool` `canConvert(string A, string B, ``int` `N)` `{` `    ` `    ` `    ``if` `(A.dimension() != B.dimension())` `        ``return` `false``;` ` `  `    ` `    ` `    ` `    ` `    ` `    ``vector<``int``> arr(26, 0);` ` `  `    ` `    ``for` `(``int` `i = 0; i < A.dimension(); i++) {` ` `  `        ` `        ` `        ``int` `shift = (B[i] - A[i] + 26) % 26;` ` `  `        ` `        ` `        ` `        ` `        ` `        ``if` `(shift != 0 && shift + arr[shift] * 26 > N)` `            ``return` `false``;` ` `  `        ` `        ` `        ``arr[shift]++;` `    ``}` ` `  `    ` `    ``return` `true``;` `}` ` `  `int` `fundamental()` `{` `    ``string A = ``"abcabc"``, B;` `    ``int` `N = 3;` ` `  `    ` `    ``if` `(canConvert(A, B, N))` `        ``cout << ``"True"``;` `    ``else` `        ``cout << ``"False"``;` ` `  `    ``return` `0;` `}`

Time Complexity: O(dimension(A))
Auxiliary Area: O(1)