Given string str, find the length of the longest repeating subsequence
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
The problem can be solved in similar way as longest Common subsequence problem, where input to LCS() would be String str both the case. That is
Let's discuss the recursive approach first.
Let,
Now,
Think of the following example,
Say the string is: x1x2...xl
Say,
xi==xj and i≠j (this is the constraint what we discussed above)
Then obviously we need to find LCS for the remaining part of string (x1x2...xi-1, x1x2...xj-1) and then add 1 for this valid character match (repeating character match),
Else
Maximum of two case,
Now, we need to recur down to 0. So,
Where base cases are,
If you generate this recursion tree, it will generate many overlapping sub-problems and thus, we need to reduce the re-computing. That's why we need to convert it into dynamic programming where we will store the output of the sub-problems and we will use it to compute bigger sub-problems.
Converting to Dynamic programming:
C++ Implementation:
Output