Data Structures & AlgorithmsVery hardCodeforces Div2 D · TCS NQT

Target string "abcbc" must be built by concatenating words from the list ['ab', 'bc', 'abc', 'a', 'b', 'c', 'cb']; each word can be reused, and the order of concatenation matters. How many different ways are there to build the target exactly?

  1. A. 7
  2. B. 12
  3. C. 10
  4. D. 8

Correct answer: C. 10

Let ways[i] be the number of ways to build the prefix of length i: ways[i] = Σ ways[i − |w|] over words w that end exactly at i. Counting only which words occur in the target ignores how many distinct concatenations exist.

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

Exams that ask this topic