Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
How many DIFFERENT non-empty subsequences does the string "abcabc" have? Two subsequences are different if their letter sequences differ.
- A. 63
- B. 51
- C. 54
- D. 3
Correct answer: B. 51
Let dp[i] count distinct subsequences (including empty) of the first i letters: dp[i] = 2·dp[i−1] − dp[last−1], where last is the previous position of the same letter. Subtracting the duplicated ones is the step that the 2ⁿ − 1 count misses.
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
- How many subsequences (chosen by index) of "aabbabcaccb" spell exactly the word abc?
- 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?