Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many subsequences (chosen by index) of "aabbabcaccb" spell exactly the word abc?
- A. 11
- B. 1
- C. 21
- D. 25
Correct answer: C. 21
Use dp[j] = number of ways to match the first j letters of the pattern so far; scanning the text, each letter adds dp[j−1] to dp[j], iterating j downwards. Counting only contiguous occurrences misses the gapped ones.
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
- Strings "ab" and "cde" are interleaved, keeping the internal order of each string. How many distinct strings can be formed this way?
- You may delete characters from "sea" and from "eat" to make the two strings equal. Deleting a character costs its ASCII code (for example…
- What is the length of the longest subsequence of [1, 7, 4, 9, 2, 5, 3, 8] whose consecutive differences alternate in sign (each step goes…
- How many contiguous subarrays of [1, 2, 3, 4, 7, 10, 13, 14, 15, 16] are arithmetic (at least 3 elements, with a constant difference…
- What is the length of the longest contiguous substring of the binary string "1100100110111000101" that has equally many 0s and 1s?
- What is the smallest positive period p of the string abcabcabca, meaning the smallest p such that s[i] = s[i+p] for every valid i?