Wednesday, November 2, 2022
HomeWordPress DevelopmentExamine if string S may be transformed to T by incrementing characters

Examine if string S may be transformed to T by incrementing characters


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 examine if S may 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)

Notice: 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’t 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 may 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).

Observe the steps beneath to implement the above concept:

  • Examine 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 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 larger 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.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 primary()

{

    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)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments