![]() |
VOOZH | about |
Given a string s, the task is to find the length of the longest repeating subsequence, such that the two subsequences don't have the same string character at the same position, i.e. any ith character in the two subsequences shouldn't have the same index in the original string.
Examples:
Input: s= "abc"
Output: 0
Explanation: There is no repeating subsequence
Input: s= "aab"
Output: 1
Explanation: The two subsequence are 'a'(0th index) and 'a'(1th index). Note that 'b' cannot be considered as part of subsequence as it would be at same index in both.
Table of Content
This problem is a variation of the longest common subsequence (LCS) problem. The idea is to treat the given string as two separate strings and find the LCS between them. However, to ensure that the subsequences are not overlapping, we add the condition that no character in the subsequences should come from the same index in the original string. This means that when comparing two characters from the two strings, they must match, and their indices must be different. By leveraging this condition, we can adapt the LCS approach to solve for the longest repeating subsequence.
The idea is to compare the characters at index i and j of s and the indices i and j. Two cases arise:
1. If the characters of the string match (s[i-1] == s[j-1]) and their indices are different (i != j), then make a recursive call for the remaining string (i-1 and j-1) and add 1 to the result.
- longestRepeatingSubsequence(i, j, s) = 1 + longestRepeatingSubsequence(i-1, j-1, s)
2. If the characters do not match or the indices are the same, make two recursive calls:
- longestRepeatingSubsequence(i, j, str) = max(longestRepeatingSubsequence(i-1, j, str), longestRepeatingSubsequence(i, j-1, str))
Base Case:
- If either of the strings becomes empty (i == 0 or j == 0), return 0.
3
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: The maximum length of repeating subsequence originating from i, j, i.e., longestRepeatingSubsequence(i, j), depends on the optimal solution of longestRepeatingSubsequence(i-1, j-1) if characters match and indices don't match. Otherwise, it depends on the maximum of longestRepeatingSubsequence(i-1, j) and longestRepeatingSubsequence(i, j-1).
2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times.
3
The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner. The idea is to create a 2-D array. Then fill the values using dp[i][j] = 1 + dp[i-1][j-1] if characters match and indices don't match. Otherwise, set dp[i][j] = max(dp[i-1][j], dp[i], [j-1]).
3
The idea is store the values for the previous row only. We can observe that for a given (i, j) its value is only dependent on (i-1, j-1) or (i-1, j) and (i, j-1).
3