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'))- A. 3 4
- B. 2 5
- C. 3 5
- 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
- What is printed? python import bisect def pairsle(a, d): a = sorted(a) c = 0 for i, x in enumerate(a): j = bisect.bisectright(a, x + d, i…
- What is printed? python import bisect def closest(a, x): i = bisect.bisectleft(a, x) cands = a[max(i - 1, 0):i + 1] return min(cands…
- What is printed? python def content(g, s): g.sort(); s.sort() i = j = 0 while i < len(g) and j < len(s): if s[j] >= g[i]: i += 1 j += 1…
- What is printed? python from collections import Counter def least(tasks, n): c = Counter(tasks) m = max(c.values()) k = sum(1 for v in…
- What is printed? python s = 'abcdefg' print(s[::-2], s[5:1:-2], s[-3::-3])
- What is printed? python a = {'x': 1, 'y': 2} b = {'y': 3, 'z': 4} c = a | b a.update(b, x=9) print(c, a)