Maximum adjacent index difference for a subsequence T present in given string S
Last Updated :
21 Feb, 2022
Given two strings S and T of lengths n and m respectively. Find the maximum adjacent index difference for the indices where string T is present in string S as a subsequence.
- Cost is defined as the difference between indices of subsequence T
- The maximum cost of a sequence is defined as max(ai+1−ai) for 1 ≤ i < m.
- It is guaranteed that there is at least subsequence as T in S.
Examples:
Input: S = “abbbc”, T = “abc”
Output: 3
Explanation: One of the subsequences is {1, 4, 5} denoting the sequence “abc” same as T, thus the maximum cost is 4 – 1 = 3 .
Input: S = “aaaaa”, T = “aa”
Output: 4
Explanation: One of the subsequences is {1, 5} and the max cost is 5 – 1 = 4 .
Approach: For some subsequence of the string S, let ai denote the position of the character Ti in the string S. For fixed i, we can find a subsequence that maximizes ai+1−ai.
Follow the below steps to solve the problem:
- Let lefti and righti be the minimum and maximum possible value of ai among all valid a’s respectively. Now, it is easy to see that the maximum possible value of ai+1 − ai is equal to righti+1 − lefti.
- To calculate lefti we just need to find the first element after lefti−1 which is equal to Ti , righti can be found in the same way by iterating in the reverse direction.
- So, after finding left and right, the answer is max(righti+1−lefti) for 1 ≤ i < m.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxCost(string s, string t, int n, int m)
{
int mxCost = 0;
int left[m], right[m];
int j = 0;
for ( int i = 0; i < n; i++) {
if (j >= m)
break ;
if (s[i] == t[j]) {
left[j] = i;
j++;
}
}
j = m - 1;
for ( int i = n - 1; i >= 0; i--) {
if (j < 0)
break ;
if (s[i] == t[j]) {
right[j] = i;
j--;
}
}
for ( int i = 0; i < m - 1; i++) {
mxCost = max(mxCost,
right[i + 1] - left[i]);
}
return mxCost;
}
int main()
{
string S = "abbbc" , T = "abc" ;
int n = S.length(), m = T.length();
cout << maxCost(S, T, n, m);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxCost(String s, String t, int n, int m)
{
int mxCost = 0 ;
int []left = new int [m];
int []right = new int [m];
int j = 0 ;
for ( int i = 0 ; i < n; i++) {
if (j >= m)
break ;
if (s.charAt(i) == t.charAt(j)) {
left[j] = i;
j++;
}
}
j = m - 1 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
if (j < 0 )
break ;
if (s.charAt(i) == t.charAt(j)) {
right[j] = i;
j--;
}
}
for ( int i = 0 ; i < m - 1 ; i++) {
mxCost = Math.max(mxCost,
right[i + 1 ] - left[i]);
}
return mxCost;
}
public static void main(String[] args)
{
String S = "abbbc" , T = "abc" ;
int n = S.length(), m = T.length();
System.out.print(maxCost(S, T, n, m));
}
}
|
Python
def maxCost(s, t, n, m):
mxCost = 0
left = []
right = []
j = 0
for i in range ( 0 , n):
if (j > = m):
break
if (s[i] = = t[j]):
left.append(i)
j + = 1
j = m - 1
for i in reversed ( range (n)):
if (j < 0 ):
break
if (s[i] = = t[j]):
right.append(i)
j - = 1
for i in range ( 0 , m - 1 ):
mxCost = max (mxCost, right[i + 1 ] - left[i])
return mxCost
S = "abbbc"
T = "abc"
n = len (S)
m = len (T)
print (maxCost(S, T, n, m))
|
C#
using System;
class GFG{
static int maxCost(String s, String t, int n, int m)
{
int mxCost = 0;
int []left = new int [m];
int []right = new int [m];
int j = 0;
for ( int i = 0; i < n; i++) {
if (j >= m)
break ;
if (s[i] == t[j]) {
left[j] = i;
j++;
}
}
j = m - 1;
for ( int i = n - 1; i >= 0; i--) {
if (j < 0)
break ;
if (s[i] == t[j]) {
right[j] = i;
j--;
}
}
for ( int i = 0; i < m - 1; i++) {
mxCost = Math.Max(mxCost, right[i + 1] - left[i]);
}
return mxCost;
}
public static void Main()
{
String S = "abbbc" , T = "abc" ;
int n = S.Length, m = T.Length;
Console.Write(maxCost(S, T, n, m));
}
}
|
Javascript
<script>
const maxCost = (s, t, n, m) => {
let mxCost = 0;
let left = new Array(m).fill(0), right = new Array(m).fill(0);
let j = 0;
for (let i = 0; i < n; i++) {
if (j >= m)
break ;
if (s[i] == t[j]) {
left[j] = i;
j++;
}
}
j = m - 1;
for (let i = n - 1; i >= 0; i--) {
if (j < 0)
break ;
if (s[i] == t[j]) {
right[j] = i;
j--;
}
}
for (let i = 0; i < m - 1; i++) {
mxCost = Math.max(mxCost,
right[i + 1] - left[i]);
}
return mxCost;
}
let S = "abbbc" , T = "abc" ;
let n = S.length, m = T.length;
document.write(maxCost(S, T, n, m));
</script>
|
Time Complexity: O(n + m)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...