Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many subsequences of 'abcabc' equal the string 'abc'? Run the DP.

Problem: How many subsequences of 'abcabc' equal the string 'abc'? Run the DP.
s, t = 'abcabc', 'abc'
dp = [1] + [0] * len(t)
for ch in s:
    for j in range(len(t), 0, -1):
        if t[j - 1] == ch:
            dp[j] += dp[j - 1]
print(dp[len(t)])
What is printed?
  1. A. 5
  2. B. 3
  3. C. 6
  4. D. 4

Correct answer: D. 4

The DP counts subsequence embeddings, processing the target from the right so each character is used at most once per step.

More Coding Interview Problems questions

Exams that ask this topic