Data Structures & AlgorithmsHard
To count distinct subsequences of a string with repeated letters, which strategy avoids double counting?
- A. Count all index subsets and divide by the number of repeated letters.
- B. Count only contiguous substrings.
- C. Process characters left to right and track the number of distinct subsequences, subtracting the count at the last occurrence of each character.
- D. Sort the string before counting.
Correct answer: C. Process characters left to right and track the number of distinct subsequences, subtracting the count at the last occurrence of each character.
Adding a character doubles the distinct subsequences, except those already produced by its previous occurrence, which must be subtracted. This recurrence counts distinct strings directly.
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
- For the 'largest number from concatenation' problem, why does the comparator compare x+y with y+x as strings?
- When choosing non-adjacent positions to maximise a sum, why does the DP keep two states per position?
- Why does the lexicographically largest subsequence after deleting k digits come from a greedy stack?
- In a sliding window tracking the maximum number of vowels among windows of fixed length 3, why is O(1) update per step possible?
- Why does a circular window of length 3 require index arithmetic modulo n?
- Why is the number of Dyck-word stack sequences with n pushes and n pops equal to a Catalan number?