Longest Repeating Subsequence | Practice | GeeksforGeeks

Given string str, find the length of the longest repeating subsequence such that it can be found twice in the given string. 
The two identified subsequences A and B can use the same ith character from string str if and only if that ith character has different indices in A and B. For example, A = "xax" and B = "xax" then the index of first "x" must be different in the original string for A and B.
Example 1:
Input:
str = "axxzxy"
Output: 2
Explanation:
The given array with indexes looks like
a x x z x y 
0 1 2 3 4 5

The longest subsequence is "xx". 
It appears twice as explained below.

subsequence A
x x
0 1  <-- index of subsequence A
------
1 2  <-- index of str 


subsequence B
x x
0 1  <-- index of subsequence B
------
2 4  <-- index of str 

We are able to use character 'x' 
(at index 2 in str) in both subsequences
as it appears on index 1 in subsequence A 
and index 0 in subsequence B.
Example 2:
Input:
str = "axxxy"
Output: 2
Explanation:
The given array with indexes looks like
a x x x y 
0 1 2 3 4

The longest subsequence is "xx". 
It appears twice as explained below.

subsequence A
x x
0 1  <-- index of subsequence A
------
1 2  <-- index of str 


subsequence B
x x
0 1  <-- index of subsequence B
------
2 3  <-- index of str 

We are able to use character 'x' 
(at index 2 in str) in both subsequences
as it appears on index 1 in subsequence A 
and index 0 in subsequence B.

Your Task:
You don't need to read or print anything. Your task is to complete the LongestRepeatingSubsequence() which takes str as input parameter and returns the length of the longest repeating subsequnece.

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

Constraints:
1 <= |str| <= 103
-239
eeshagoyal2Admin10 months ago
The problem statement has been updated to avoid any ambiguity. 
0
10ayushshuklain 8 hours
LCS DP C++
 
 
int LongestRepeatingSubsequence(string str){     // Code here     int n=str.length();     int t[n+1][n+1];     string S=str;     for(int i=0;i<=n;i++){         for(int j=0;j<=n;j++){             if(i==0||j==0){                 t[i][j]=0;             }         }     }          for(int i=1;i<=n;i++){         for(int j=1;j<=n;j++){             if(str[i-1]==S[j-1] && i!=j){                 t[i][j]=1+t[i-1][j-1];             }             else{                 t[i][j]=max(t[i-1][j],t[i][j-1]);             }         }     }     return t[n][n]; }
0
user_hix315 hours ago
int LongestRepeatingSubsequence(string str){
		    // Code here
		    int n = str.size();
		    int dp[n+1][n+1];
		    memset( dp, 0, sizeof(dp) );// this is base case 
		    for(int i=1;i<=n;i++){
		        for(int j=1;j<=n;j++){
		            if(str[i-1] == str[j-1] && i!=j){ //same character differnt index(similar to LCS)   
		                    dp[i][j] = dp[i-1][j-1]+1;
		            }
		            else{
		               dp[i][j] = max(dp[i-1][j],dp[i][j-1]);// usual dp concept of top down approch
		            }
		        }
		    }
		    return dp[n][n];// last corner will give solution top down concept
		}
0
user_r1cs3 days ago
class Solution {public: int LongestRepeatingSubsequence(string str){     int n=str.length();     string str1=str;     string str2=str;     int t[n+1][n+1];     for(int i=0;i<n+1;i++){         for(int j=0;j<n+1;j++){             if(i==0){                 t[i][j]=0;             }             if(j==0){                 t[i][j]=0;             }         }     }      for(int i=1;i<n+1;i++){         for(int j=1;j<n+1;j++){         if(str1[i-1]==str2[j-1] && i!=j){             t[i][j]=1+t[i-1][j-1];         }         else{             t[i][j]=max(t[i-1][j],t[i][j-1]);         }                      }         }     return t[n][n]; }
};
0
absurajsinghchamiyal4 days ago
Neat and clean code 
yaa!  it's easy if you are GOD!
 
//--------TABULATION// Varaition of LCS // Hint : take two str (same) and find LCS such that i!=j
public class Solution {   public int LongestRepeatingSubsequence(String str) {       int n = str.length();       int dp[][] = new int[n + 1][n + 1];       for (int i = 1; i <= n; i++) {           for (int j = 1; j <= n; j++) {               if (i != j && str.charAt(i - 1) == str.charAt(j - 1))                   dp[i][j] = 1 + dp[i - 1][j - 1];               else                   dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);           }       }       return dp[n][n];   }}// Time complexity : O(n^2)// Space complexity : O(n^2)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.