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?- A. 8
- B. 1
- C. 2
- 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
- Array a = [2, 3, 1, 1, 4, 2, 0, 3, 1, 1]; from index i you may jump to any index in [i+1, i+a[i]]. Find the minimum number of jumps from…
- Trapping rain water: heights h = [4, 2, 0, 3, 2, 5, 1, 0, 2]. Compute the total water trapped between the bars. python…
- The permutation p = [4, 3, 1, 7, 6, 2, 5] of 1..7 must be sorted by swaps of any two elements. What is the minimum number of swaps?…
- Count the lattice paths of length 8 with steps +1 and −1 that start at 0, never go below 0, and end at 0. Run the DP over positions and…
- How many ways are there to tile a 3 × 8 board completely with 1 × 2 dominoes (placed horizontally or vertically)? Count by DFS over the…
- Meetings are given as half-open intervals [(0,30),(5,10),(15,20),(12,25),(28,35)]. What is the minimum number of meeting rooms needed so…