educative.io

Educative

What would be the easiest way to return the longest substring or subsequence ?. Is there a pattern that we could follow?

Hi,

I have a quick question. I see that all these problems return the length of the longest subsequence/substring. Is there a way we could print/return the subsequence/substring too by using backtracking or other approaches using the same LPS function defined in this course or do we need to define a separate method to retrieve the sequence. Please let me know.

Hi Gautham,

We can use a similar solution to return the string instead. See this, we have presented a solution to return the string:

A similar approach can be followed for subsequence too. For subsequences, we need to keep track of the current longest subsequence in an array instead.

Hope this answers your question.

1 Like

Oh, I see. Thank you