[Algorithm] Longest Repeated Subsequence (DP/C++)

Changjin
1 min readJan 3, 2021

Hello! Today we’ll be looking at another DP question. This is a variation of LCS and a slight modification of LCS will hold.

Longest Repeated Subsequence is a subsequence of a string such that it appears twice but those two identical subsequences DO NOT have any same character at the same position. Let’s take an example, consider “AAEFBB”.

-> A A E F B B
-> A A E F B B

AB is a subsequence of “AAEFBB” which appears “twice” in the original string but they do not have any overlapping index.

Longest Common Sequence(LCS) can be used to find the LRS. Recall that LCS is a common subsequence of two strings with the maximum length. As you can notice from the example above, finding the LRS is simply finding the LCS but with no characters at the same position of the two strings.

The code below will give you clear ideas

Time Complexity: O(n²)

The only difference is line 10 . When i==j, skip it.

[Contact Info]
email
: changjin9792@gmail.com

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Changjin
Changjin

No responses yet

Write a response