Maximum adjacent index difference for a subsequence T present in given string S


In this problem, we need to find the maximum difference between the indices of the subsequence present in the given string.

To solve the problem, we need to find the indices of the subsequence in the actual and reverse order. After that, we need to take the difference of both.

Problem statement - We have given two strings named 'str' and 'sub'. The 'sub' string is always present in the 'str' string as a subsequence. We need to find the maximum index cost. The index cost is the difference between two indices of the subsequence.

Sample examples

Input

str = "deeer", sub = "der"

Output

3

Explanation

  • We can get the 'sub' using the {0, 1, 4}, {0, 2, 4}, and {0, 3, 4}.

  • So, if we take set {0, 1, 4}, we can get the maximum adjacent index difference which is 4 - 1 == 3.

Input

str = "abcdef", sub = "acf"

Output

3

Explanation

  • The subsequence is present at the {0, 2, 5} indices. So, the maximum difference between indices is 5 - 2 = 3.

Input

str = "bbbbbbb", sub = "bb";

Output

6

Explanation - We can take {0, 6} indices of subsequence to get the maximum difference between adjacent indices.

Approach 1

In this approach, we will find the subsequence indices in the actual order. After that, we will find the subsequences indices in the reverse order. We will subtract the actual indices from reverse indices to get the maximum difference.

Let's understand the approach via the example below.

str = "bbbbbbb", sub = "bbb";

  • When we find the subsequence indices in the actual order, we will get {0, 1, 2} indices.

  • When we find the subsequence indices in the reverse order, we will get {4, 5, 6}.

  • At last, we will subtract the reverse[p + 1] - actual[p] = 6 - 1 = 5. Also, 5 - 0 = 5. So, in this way, we can get the maximum adjacent index difference.

Algorithm

Step 1 - Define the 'max_diff' variable to store the maximum adjacent index difference. Also, define the leftInd and rightInd array of length equal to the substring length to store the indexes of the subsequence from the left and right.

Step 2 - Start traversing the given string. Break the loop if a pointer to the 'sub' string is greater than the string length.

Step 3 - If the character at the pth index in the string and sub_ind index in the 'sub' string is the same, assign the p-value to the 'leftInd' array and increment the sub_ind pointer.

Step 4 - Initialize the sub_ind pointer with substring length - 1, and start traversing the string in the reverse order.

Step 5 - If sub_ind is less than 0, break the loop. Furthermore, if the character of the string and 'sub' matches, update the 'rightInd' array and decrement the 'sub_ind' value by 1.

Step 6 - traverse the 'leftInd' and 'rightInd' arrays. Update the value of the maxDiff variable if rightInd[p + 1] - leftInd[p] is greater than it.

Step 7 - Return the 'maxDiff' value.

Example

#include <bits/stdc++.h>
using namespace std;

int findMaxIndexDiff(string str, string sub, int str_len, int sub_len) {
   // to store maximum index difference
   int maxDiff = 0;
   // to store minimum and maximum possible values of the character index
   int leftInd[sub_len], rightInd[sub_len];

   // pointer for substring
   int sub_ind = 0;
   // traverse the string
   for (int p = 0; p < str_len; p++) {
      if (sub_ind >= sub_len)
         break;
// store the left-most index of the character of a substring in the given string
      if (str[p] == sub[sub_ind]) {
         leftInd[sub_ind] = p;
         sub_ind++;
      }
   }
   sub_ind = sub_len - 1;
   for (int p = str_len - 1; p >= 0; p--) {
      if (sub_ind < 0)
         break;
      // Storing right-most index of character
      if (str[p] == sub[sub_ind]) {
         rightInd[sub_ind] = p;
         sub_ind--;
      }
   }
   for (int p = 0; p < sub_len - 1; p++){
      // Getting the maximum difference
      maxDiff = max(maxDiff, rightInd[p + 1] - leftInd[p]);
   }
   // Return the final answer
   return maxDiff;
}
int main() {
   string str = "deeer", sub = "der";
   int str_len = str.length(), sub_len = sub.length();
   cout << "The maximum index difference for a subsequence present in the given string is " << findMaxIndexDiff(str, sub, str_len, sub_len);
   return 0;
}

Output

The maximum index difference for a subsequence present in the given string is 3

Time complexity - O(n + m) to traverse the string and array of indices of subsequence.

Space complexity - O(M) to store indices of subsequence.

We can take any occurrence of subsequence and find the maximum difference between its adjacent indices. Programmers should test the code with different inputs to better understand the problem and its solution.

Updated on: 24-Aug-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements