Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: From the string "aaabbbaabbccaa", you may delete the minimum number of characters so that no two adjacent characters are equal. How many characters must be deleted?

  1. A. 6
  2. B. 14
  3. C. 11
  4. D. 8

Correct answer: D. 8

Runs are aaa, bbb, aa, bb, cc, aa; each run keeps one letter, so deletions = 2 + 2 + 1 + 1 + 1 + 1 = 8. Counting runs gives 6, deleting every letter in a repeated run gives 14, and length minus distinct letters (14 − 3) gives 11.

More Coding Interview Problems questions

Exams that ask this topic