Maximum point to convert string S to T by swapping adjacent characters


In this problem, we will find the maximum points while converting the string S to T according to the conditions in the problem statement.

We can traverse the string S and make each character of the string S same as string T's character at the same index by maximum swaps to get maximum points.

In the other approach, we will prepare a mathematical formula based on the string's observation to get the answer.

Problem statement - We have given a string S and T containing the alphabetical and numeric characters. We need to count the maximum points while converting the string S to T.

To get ($\mathrm{S_{p}-s_{p}+1}$) points, we need to swap the $\mathrm{S_{p}}$ and $\mathrm{S_{p}+1}$ element of the string S.

Sample examples

Input

S = "543"; T = "345";

Output

4

Explanation

  • First, swap (4, 3) to get 1 point, and the string will be 534.

  • Next, swap the (5, 3) to get 2 points, and the string will be 354.

  • In the last move, swap (5, 4) to get the 1 point, and the string will become the same as T.

Input

S = "1234"; T = "1234";

Output

0

Explanation - Both strings are already the same, so we get 0 points.

Input

S = "179"; T = "125";

Output

-1

Explanation - In any case, we can't convert string S to T by swapping characters. So, it prints -1.

Approach 1

In this approach, if the characters at the pth index of both strings don't match, we find the next index of the matching character in the string S. After that, we swap the matching character with string characters to place it at the pth index and sum the gained points. In this way, we replace each unmatching character of string S and take the sum of their points.

Algorithm

Step 1 - Initialize the 'pnt' with 0 to store the maximum points.

Step 2 - Start traversing the string.

Step 3 - If the character at the pth index in the string S and T is same, move to the next iteration.

Step 4 - Otherwise, find the next index of the T[p] in the string S. If a character is not found, return -1, as we can't convert the string S to T.

Step 5 - Use the while loop to make iterations until q > p to swap string characters.

Step 6 - Add the S[q - 1] - S[q] to the 'pnt' value, which is gained a point for the swap of the S[q - 1] and S[q] characters.

Step 7 - Swap the S[q - 1] and S[q] characters using the swap() method. Also, decrement the value of q by 1.

Step 8 - Return the 'pnt' value.

Example

#include <iostream>
using namespace std;

int getMaximumPoint(string S, string T, int str_len) {
   int pnt = 0;
   for (int p = 0; p < str_len; p++) {
     // When characters are the same at the same index
     if (S[p] == T[p]) {
       continue;
     }
     int q = p + 1;
     // Finding the index of the next matching character
     while (q < str_len && S[q] != T[p]){
       q++;
     }
     // If no matching character is found
     if (q == str_len){
       return -1;
     }
     // Swap characters and get points
     while (q > p) {
       pnt += S[q - 1] - S[q];
       swap(S[q], S[q - 1]);
       q--;
     }
   }
   // Return total points
   return pnt;
}

int main() {
   string S = "543";
   string T = "345";
   int str_len = S.length();
   cout << "The maximum pnt we can get while converting string S to T is " 
<<getMaximumPoint(S, T, str_len) << endl;
   return 0;
}

Output

The maximum pnt we can get while converting string S to T is 4

Time complexity - O(N*N) to traverse string and find the next index of matching character.

Space complexity - O(1), as we don't use any extra space.

Approach 2

In this approach, we will create a formula to get the output of the problem.

When we swap the $\mathrm{S_{p}}$ and $\mathrm{S_{p}+1}$ character, the points we get are equal to the ($\mathrm{S_{p}+1-s_{p}}$).

Suppose the initial position of the character is p, and after swapping, the character's position is q.

Whenever we swap $\mathrm{S_{p} + 1}$ and $\mathrm{S_{p}}$, the cost increases by  ($\mathrm{S_{p}+1-s_{p}}$). Here, q increases by 1 in every swap.

Here, we can say that whenever q increases by 1, the points increase by Sp, and whenever q decrease by 1, the points decrease by $\mathrm{S_{p}}.

So, the final cost is $\mathrm{S_{p}(q - p)}$ for a single character.

Here, $\mathrm{S_{p} * q}$ is equal to the $\mathrm{T_{p} * p}$ as we make string S equal to T.

So, the cost for the single character is $\mathrm{T_{p} * p - S_{p} * p}$, where 'p' is the particular index.

Here is the final formula to get the output.

Sum($\mathrm{T_{p} * p - S_{p} * p}$), where 1 <= p <= string length

Algorithm

Step 1 - Start traversing the string.

Step 2 - Multiply the character value with its index for T and S string, and subtract the T value from S. After that, add the resultant value to the 'pnt' variable.

Step 3 - Return the 'pnt' value.

Example

#include <iostream>
using namespace std;

long getMaximumPoint(string S, string T, int str_len) {
   // To store the sum of the multiplication of each digit and its index
   int pnt = 0;
   for (int p = 0; p < str_len; p++) {
     pnt += (T[p] - '0') * 1l * (p + 1) - (S[p] - '0') * 1l * (p + 1);
   }
   return pnt;
}
int main() {
   string S = "543";
   string T = "345";
   int str_len = S.length();
   cout << "The maximum pnt we can get while converting string S to T is " 
<< getMaximumPoint(S, T, str_len) << endl;
   return 0;
}

Output

The maximum pnt we can get while converting string S to T is 4

Time complexity - O(N) to traverse the string.

Space complexity - O(1), as we don't use any dynamic space.

In the second approach, we created the formula to solve the problem based on the observation. It will also work for the strings containing the same character multiple times as with any occurrence of the element we swap another element, cost remains the same.

Updated on: 29-Aug-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements