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?- A. 5
- B. 3
- C. 6
- 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
- Problem: How many strings of length 5 over {a, b, c} have no two adjacent equal characters? Run the code. python from itertools import…
- Problem: How many inversions does [9, 7, 5, 3, 1, 2, 4, 6, 8] have? Run the code. python a = [9, 7, 5, 3, 1, 2, 4, 6, 8] print(sum(1 for i…
- Problem: What is the maximum number of intervals from [[1,5],[2,6],[4,8],[7,9]] that contain a common point? Run the sweep code. python iv…
- Problem: What is the minimum number of coins from {1, 7, 10} that sum to 99? Run the DP. python INF = 10 9 d = [0] + [INF] 99 for v in…
- Problem: How many palindromic numbers below 10^4 are divisible by 11? Run the code. python print(sum(1 for x in range(1, 10 4) if str(x)…
- Problem: How many integer triples (a, b, c) with 1 <= a <= b <= c <= 10 have a b c = 36? Run the code. python print(sum(1 for a in…