Data Structures & AlgorithmsHardTCS CodeVita · Microsoft SDE OA

Count the subsequences (by position, not distinct strings) of "abcabc" that equal "abc". Run the DP that counts matches.

Count the subsequences (by position, not distinct strings) of "abcabc" that equal "abc". Run the DP that counts matches.
s="abcabc"; t="abc"
dp=[1]+[0]*len(t)
for ch in s:
    for k in range(len(t),0,-1):
        if t[k-1]==ch: dp[k]+=dp[k-1]
print(dp[len(t)])
What is printed?
  1. A. 8
  2. B. 1
  3. C. 2
  4. D. 4

Correct answer: D. 4

Process characters once and update counts from the longest prefix down so no character is reused. Counting distinct strings collapses all occurrences into one, counting contiguous blocks misses gapped matches, and multiplying letter counts ignores the required order.

More Coding Interview Problems questions

Exams that ask this topic