logo CodeStepByStep logo

longestRepeatedSubsequence

Language/Type: C++ dynamic programming

Write a function named longestRepeatedSubsequence that uses dynamic programming to find and return the longest subsequence that occurs twice in a given string. Recall that if a string is a subsequence of another, each of its letters occurs in the longer string in the same order, but not necessarily consecutively. For example, if the string is "AABEBCDD", the longest repeated subsequence is "ABD" which occurs twice, once at indexes 0-2-6 and once at indexes 1-4-7. For the string "ABA", the longest repeated subsequence is "A". Note that the two subsequences may not reuse any character indexes from the original string; for example, in "ABABC", the sequence "ABC" does not occur twice because each occurrence would have to share the "C" at index 4. If the string does not contain any repeated subsequences, return "".

The key constraint of this problem is that you must solve it using a bottom-up dynamic programming approach. Do not use recursion. You are allowed to construct any data structures (array, vector, set, map, etc.) necessary to store the data for your dynamic programming algorithm. You are also allowed to define other "helper" functions if you like.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.