Find two equal subsequences of maximum length with at least one different index
Last Updated :
27 Dec, 2022
Given a string str, the task is to find the maximum length K such that there exist two sub-sequences A and B each of length K such that A = B and number of common indices between A and B is at most K – 1.
Examples:
Input: str = “geeksforgeeks”
Output: 12
The two subsequences are
str[0…1] + str[3…12] = “geksforgeeks”
and str[0] + str[2…12] = “geksforgeeks”.
Input: str = “abcddefg”
Output: 7
Approach: Find any pair of the same letter with a minimum number of letters between them let say this minimum number be X, now the answer of the problem is len(str) – (X + 1). One is added in X to not take count of one letter from the pair.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 26;
int maxLength(string str, int len)
{
int res = 0;
int lastPos[MAX];
for ( int i = 0; i < MAX; i++) {
lastPos[i] = -1;
}
for ( int i = 0; i < len; i++) {
int C = str[i] - 'a' ;
if (lastPos[C] != -1) {
res = max(len - (i - lastPos[C] - 1) - 1, res);
}
lastPos[C] = i;
}
return res;
}
int main()
{
string str = "geeksforgeeks" ;
int len = str.length();
cout << maxLength(str, len);
return 0;
}
|
Java
class GFG
{
static int MAX = 26 ;
static int maxLength(String str, int len)
{
int res = 0 ;
int lastPos[] = new int [MAX];
for ( int i = 0 ; i < MAX; i++)
{
lastPos[i] = - 1 ;
}
for ( int i = 0 ; i < len; i++)
{
int C = str.charAt(i) - 'a' ;
if (lastPos[C] != - 1 )
{
res = Math.max(len - (i -
lastPos[C] - 1 ) - 1 , res);
}
lastPos[C] = i;
}
return res;
}
public static void main(String args[])
{
String str = "geeksforgeeks" ;
int len = str.length();
System.out.println(maxLength(str, len));
}
}
|
Python3
MAX = 26 ;
def maxLength( str , len ):
res = 0 ;
lastPos = [ 0 ] * MAX ;
for i in range ( MAX ):
lastPos[i] = - 1 ;
for i in range ( len ):
C = ord ( str [i]) - ord ( 'a' );
if (lastPos[C] ! = - 1 ):
res = max ( len - (i - lastPos[C] - 1 ) - 1 , res);
lastPos[C] = i;
return res;
if __name__ = = '__main__' :
str = "geeksforgeeks" ;
len = len ( str );
print (maxLength( str , len ));
|
C#
using System;
class GFG
{
static int MAX = 26;
static int maxLength( string str, int len)
{
int res = 0;
int []lastPos = new int [MAX];
for ( int i = 0; i < MAX; i++)
{
lastPos[i] = -1;
}
for ( int i = 0; i < len; i++)
{
int C = str[i] - 'a' ;
if (lastPos[C] != -1)
{
res = Math.Max(len - (i -
lastPos[C] - 1) - 1, res);
}
lastPos[C] = i;
}
return res;
}
public static void Main()
{
string str = "geeksforgeeks" ;
int len = str.Length;
Console.WriteLine(maxLength(str, len));
}
}
|
Javascript
<script>
var MAX = 26;
function maxLength(str, len)
{
var res = 0;
var lastPos = Array(MAX);
for ( var i = 0; i < MAX; i++) {
lastPos[i] = -1;
}
for ( var i = 0; i < len; i++) {
var C = str[i].charCodeAt(0) - 'a' .charCodeAt(0);
if (lastPos[C] != -1) {
res = Math.max(len - (i - lastPos[C] - 1) - 1, res);
}
lastPos[C] = i;
}
return res;
}
var str = "geeksforgeeks" ;
var len = str.length;
document.write( maxLength(str, len));
</script>
|
Time Complexity: O(n) where n is the length of the input string.
Auxiliary Space: O(26) ⇒ O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...