Data Structures & AlgorithmsVery hard
When counting subsequences 'ace' in a string, why does a DP over the pattern prefixes avoid double counting?
- A. Subsequences are always counted twice by definition.
- B. Only the first letter is used in the DP.
- C. The DP uses sorting so duplicate indices collapse.
- D. Each index is matched to a unique pattern position in increasing order, so each index triple is counted exactly once.
Correct answer: D. Each index is matched to a unique pattern position in increasing order, so each index triple is counted exactly once.
Processing the string left to right and extending the count of matched prefixes gives each index tuple once, because positions are consumed in increasing order. This is the standard subsequence-counting recurrence.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- To count subarrays with equal numbers of even and odd values, which transformation makes the problem a zero-sum subarray problem?
- The function below computes the length of a longest increasing subsequence. python import bisect a = [4, 4, 1, 5, 5, 6, 2, 7] t = [] for x…
- A maximum subarray sum routine is written as follows. python def bestsub(a): cur = best = a[0] for x in a[1:]: cur = max(x, cur + x) best…
- A knapsack routine is written as follows (items are (weight, value)). python items = [(2, 3), (3, 4), (4, 5)] W = 6 dp = [0] (W + 1) for…
- Two DP loops count ways to pay 5 using coins 1 and 2 (the order of coins in the sum is ignored in the first version). python coins = [1…
- A monotonic deque computes the minimum of each window of size 3. python from collections import deque a = [4, 2, 12, 11, -5, 7] k = 3 dq =…