Data Structures & AlgorithmsHardGATE CS · TCS NQT
Words available: a, ab, b, ba, aa, abba, bab, aab. How many different ways can the string 'abbaaab' be split into a sequence of these words, where each word is used in order as a contiguous piece?
- A. 19
- B. 7
- C. 25
- D. 24
Correct answer: D. 24
Let ways[i] count splits of the prefix of length i: ways[i] = Σ ways[i − |w|] over words w that end exactly at position i. Counting only distinct words that appear in s, or mistakenly treating the answer as a yes/no check, gives a different result.
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
- A rod of length 8 can be cut into integer pieces. A piece of length i sells for price[i], where price = [0, 1, 5, 8, 9, 10, 17, 17, 20]…
- In how many ways can 12 be written as a sum of DISTINCT positive integers, where order does not matter (so 12 itself counts as one way)?
- Balloons with values [3, 1, 5, 8, 2] are burst one at a time. Bursting balloon i gives coins = left × value[i] × right, where left and…
- How many non-empty contiguous subarrays of [4, -2, 7, -5, 3, 6] have a sum of at most 4?
- Starting from n = 191, in each step you may replace n by n/2 when n is even, or by n−1 or n+1 when n is odd. What is the minimum number of…
- Coins of value 2, 3 and 5 are available in unlimited supply. In how many ways can you pay exactly 10 when the ORDER of coins does NOT…