logo CodeStepByStep logo


Language/Type: Java dynamic programming

Write a method 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, list, set, map, etc.) necessary to store the data for your dynamic programming algorithm. You are also allowed to define other "helper" methods if you like.

Type your Java solution code here:

This is a method exercise. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.