ProgrammingVery hardGATE CS & IT · Infosys

What is printed?

What is printed?
def distinct(s, t):
    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]
    return dp[len(t)]
print(distinct('rabbbit', 'rabbit'), distinct('babgbag', 'bag'))
  1. A. 3 4
  2. B. 2 5
  3. C. 3 5
  4. D. 4 5

Correct answer: C. 3 5

dp[j] counts the ways the first j letters of t appear as subsequences of the part of s read so far. rabbbit contains rabbit in 3 ways, and babgbag contains bag in 5 ways.

More Python Programming questions

Exams that ask this topic