Verify if string S might be transformed to T by incrementing characters

[ad_1]

View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

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’.

An strategy 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 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:

  • Verify if the scale of each strings shouldn’t be equal
  • Initialize an array arr[] of measurement 26. the place, arr[i] = x implies that there are x characters in string A that want an increment of i occasions 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 <bits/stdc++.h>

utilizing namespace std;

  

bool canConvert(string A, string B, int N)

{

    

    

    if (A.measurement() != B.measurement())

        return false;

  

    

    

    

    

    

    vector<int> arr(26, 0);

  

    

    for (int i = 0; i < A.measurement(); i++) {

  

        

        

        int shift = (B[i] - A[i] + 26) % 26;

  

        

        

        

        

        

        if (shift != 0 && shift + arr[shift] * 26 > N)

            return false;

  

        

        

        arr[shift]++;

    }

  

    

    return true;

}

  

int foremost()

{

    string A = "abcabc", B;

    int N = 3;

  

    

    if (canConvert(A, B, N))

        cout << "True";

    else

        cout << "False";

  

    return 0;

}

Time Complexity: O(measurement(A))
Auxiliary House: O(1)

[ad_2]

Leave a Reply