Given two sequences 𝑋 and π‘Œ: 𝑋 = βŸ¨π‘Ž,𝑏,𝑐,𝑏,𝑑,π‘Ž,π‘βŸ© π‘Œ =…

2015

Given two sequences 𝑋 andΒ π‘Œ:

𝑋 = βŸ¨π‘Ž,𝑏,𝑐,𝑏,𝑑,π‘Ž,π‘βŸ©

π‘Œ = βŸ¨π‘,𝑑,𝑐,π‘Ž,𝑏,π‘ŽβŸ©

The longest common subsequence of X and Y is:

  1. A.

    βŸ¨π‘,𝑐,π‘ŽβŸ©

  2. B.

    βŸ¨π‘,π‘Ž,π‘βŸ©

  3. C.

    βŸ¨π‘,𝑐,π‘Ž,π‘ŽβŸ©

  4. D.

    βŸ¨π‘,𝑐,𝑏,π‘ŽβŸ©

Attempted by 271 students.

Show answer & explanation

Correct answer: D

Solution: The longest common subsequence is βŸ¨π‘,𝑐,𝑏,π‘ŽβŸ© (length 4).

  • Matching indices in X = ⟨a, b, c, b, d, a, b⟩: positions 2(b), 3(c), 4(b), 6(a).

  • Matching indices in Y = ⟨b, d, c, a, b, a⟩: positions 1(b), 3(c), 5(b), 6(a).

  • This gives the common subsequence βŸ¨π‘,𝑐,𝑏,π‘ŽβŸ© in both sequences.

Why it is longest:

  • Other candidate subsequences such as βŸ¨π‘,𝑐,π‘ŽβŸ© or βŸ¨π‘,π‘Ž,π‘βŸ© have length 3 and can be extended to βŸ¨π‘,𝑐,𝑏,π‘ŽβŸ©, so they are not maximal.

  • Sequences that try to include two 'a' characters in order (for example βŸ¨π‘,𝑐,π‘Ž,π‘ŽβŸ©) fail because X does not have the required two 'a' occurrences after the chosen b and c in the correct order.

  • Therefore no common subsequence of length 5 exists, and βŸ¨π‘,𝑐,𝑏,π‘ŽβŸ© (length 4) is a longest common subsequence.

Explore the full course: Coding For Placement