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?

  1. A. 19
  2. B. 7
  3. C. 25
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic