# Longest Repeated Subsequence

Given a string, print the longest repeating subseequence such that the two subsequence don’t have same string character at same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string.

More Examples:

```Input: str = "aabb"
Output: "ab"

Input: str = "aab"
Output: "a"
The two subssequence are 'a'(first) and 'a'
(second). Note that 'b' cannot be considered
as part of subsequence as it would be at same
index in both.
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

This problem is just the modification of Longest Common Subsequence problem. The idea is to find the LCS(str, str) where str is the input string with the restriction that when both the characters are same, they shouldn’t be on the same index in the two strings.

We have discussed a solution to find length of the longest repeated subsequence.

```// Refer http://www.geeksforgeeks.org/longest-repeating-subsequence/
// for complete code.
// This function mainly returns LCS(str, str)
// with a condition that same characters at
// same index are not considered.
int findLongestRepeatingSubSeq(string str)
{
int n = str.length();

// Create and initialize DP table
int dp[n+1][n+1];
for (int i=0; i<=n; i++)
for (int j=0; j<=n; j++)
dp[i][j] = 0;

// Fill dp table (similar to LCS loops)
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
// If characters match and indexes are
// not same
if (str[i-1] == str[j-1] && i != j)
dp[i][j] =  1 + dp[i-1][j-1];

// If characters do not match
else
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[n][n];
}
```

How to print the subsequence?
The above solution only finds length of subsequence. We can print the subsequence using dp[n+1][n+1] table built. The idea is similar to printing LCS.

```
// Pseudo code to find longest repeated
// subsequence using the dp[][] table filled
// above.

// Initialize result
string res = "";

// Traverse dp[][] from bottom right
i = n, j = n;
while (i > 0 && j > 0)
{
// If this cell is same as diagonally
// adjacent cell just above it, then
// same characters are present at
// str[i-1] and str[j-1]. Append any
// of them to result.
if (dp[i][j] == dp[i-1][j-1] + 1)
{
res = res + str[i-1];
i--;
j--;
}

// Otherwise we move to the side
// that that gave us maximum result
else if (dp[i][j] == dp[i-1][j])
i--;
else
j--;
}

// Since we traverse dp[][] from bottom,
// we get result in reverse order.
reverse(res.begin(), res.end());

return res;
```

Below is implementation of above steps.

```// C++ program to find the longest repeated
// subsequence
#include <bits/stdc++.h>
using namespace std;

// This function mainly returns LCS(str, str)
// with a condition that same characters at
// same index are not considered.
string longestRepeatedSubSeq(string str)
{
// THIS PART OF CODE IS SAME AS BELOW POST.
// IT FILLS dp[][]
// http://www.geeksforgeeks.org/longest-repeating-subsequence/
// OR the code mentioned above.
int n = str.length();
int dp[n+1][n+1];
for (int i=0; i<=n; i++)
for (int j=0; j<=n; j++)
dp[i][j] = 0;
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
if (str[i-1] == str[j-1] && i != j)
dp[i][j] =  1 + dp[i-1][j-1];
else
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);

// THIS PART OF CODE FINDS THE RESULT STRING USING DP[][]
// Initialize result
string res = "";

// Traverse dp[][] from bottom right
int i = n, j = n;
while (i > 0 && j > 0)
{
// If this cell is same as diagonally
// adjacent cell just above it, then
// same characters are present at
// str[i-1] and str[j-1]. Append any
// of them to result.
if (dp[i][j] == dp[i-1][j-1] + 1)
{
res = res + str[i-1];
i--;
j--;
}

// Otherwise we move to the side
// that that gave us maximum result
else if (dp[i][j] == dp[i-1][j])
i--;
else
j--;
}

// Since we traverse dp[][] from bottom,
// we get result in reverse order.
reverse(res.begin(), res.end());

return res;
}

// Driver Program
int main()
{
string str = "AABEBCDD";
cout << longestRepeatedSubSeq(str);
return 0;
}
```

Output:

`ABD`

Time Complexity : O(n2)
Auxiliary Space : O(n2)

This article is contributed by Kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.2 Average Difficulty : 3.2/5.0
Based on 10 vote(s)