Write a function named longestRepeatedSubsequence
that uses recursive backtracking 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 two or more subsequences tie for longest repeated subsequence, you may return any of them.
If the string does not contain any repeated subsequences, return ""
.
Your function must be recursive.
You are allowed to use loops in your solution so long as your overall algorithm is based on recursion and backtracking.
You are allowed to construct any data structures (array, vector, set, map, etc.) necessary to store the data for your algorithm.
You are also allowed to define other "helper" functions if you like.