Data Structures & AlgorithmsVery hard
How many index triples i < j < k in "abcdeacace" spell the subsequence 'ace' (characters need not be adjacent)?
- A. 1
- B. 18
- C. 7
- D. 6
Correct answer: C. 7
Count the index triples in order, which is a DP over the pattern: the number of ways to match each prefix of the pattern. Counting contiguous occurrences, multiplying letter counts ignores order, and counting pairs drops the last letter.
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 index triples i < j < k in "abacaba" give a palindromic subsequence S[i]S[j]S[k] of length 3 (that is, S[i] = S[k])?
- How many contiguous subarrays of 2 1 4 3 6 5 8 7 of length at least 2 contain EQUAL numbers of even and odd values?
- What is the length of the longest contiguous subarray of 3 6 9 4 12 18 5 in which EVERY element is divisible by the FIRST element of that…
- What is the maximum sum of a contiguous subarray of 3 1 3 2 4 1 5 whose elements are all DISTINCT?
- What is the MINIMUM sum of a contiguous subarray of 4 -2 1 -5 3 2 -1 whose length is at least 3?
- In the string "cbaebabacd", how many substrings of length 3 are ANAGRAMS of "abc" (same letters, any order)?