Practice free →
HomeGATE CSEcomputerscienceAlgorithms › If the last characters of strings X and Y are eq…

If the last characters of strings X and Y are equal, LCS recurrence says

ALCS(X, Y) = max(LCS(X without last, Y), LCS(X, Y without last))
BLCS(X, Y) = LCS(X without last, Y without last) + that character
CLCS(X, Y) = LCS(X without last, Y without last) (skipping the character)
DLCS(X, Y) = the empty string
Answer & Solution
Correct answer: B. LCS(X, Y) = LCS(X without last, Y without last) + that character
When x_m = y_n, that character is guaranteed in the LCS, and the rest of the LCS comes from the LCS of the two strings minus their last characters. Hence LCS(X, Y) = LCS(X_{m-1}, Y_{n-1}) + x_m. The max-of-two formula is for the case x_m ≠ y_n; skipping the matching character or returning empty is wrong.
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions