Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 C
Problem: How many subsequences equal to the string "abc" are contained in "aabbcbcac" (a subsequence picks positions in increasing order, not necessarily contiguous)?
- A. 27
- B. 6
- C. 16
- D. 18
Correct answer: C. 16
Scan once, adding count('ab' so far) at each c and count('a' so far) at each b: the c's at positions 4, 6 and 8 contribute 4 + 6 + 6 = 16. Multiplying the letter counts 3 × 3 × 3 ignores order (27), counting 'ab' alone gives 6, and multiplying all 'ab' pairs by the 3 c's ignores that each c must come after its b (18).
More Coding Interview Problems questions
- Problem: The string "aabcccccaaa" is run-length encoded (each run becomes its letter followed by its run length, e.g. "a2b1..."). What is…
- Problem: Find the length of the shortest substring of "xaybzcxyabc" that contains all the letters a, b and c at least once.
- Problem: A Conway's Game of Life grid on a 4×4 board with alive cells at (1,0), (1,1), (1,2), (0,2) is advanced by one generation…
- Problem: A list of 6 customers arrives at a bank counter with service times 4, 1, 3, 2, 6, 5 (in that arrival order). The counter serves…
- Problem: A linked list of 10 nodes is traversed with Floyd's tortoise and hare: the slow pointer moves 1 step and the fast pointer moves 2…
- Problem: An LRU cache holds 3 entries. Keys are accessed in this order: 1, 2, 3, 1, 4, 2, 5, 1, 3, 4. A key is a hit if it is already…