Data Structures & AlgorithmsHard

From the digit string "53412", delete EXACTLY two characters (the remaining characters keep their order). What is the lexicographically LARGEST string that can remain?

  1. A. 245
  2. B. 345
  3. C. 543
  4. D. 542

Correct answer: D. 542

Greedily delete a smaller digit that is followed by a larger one, using a stack, which keeps the earliest digits as large as possible. Reversing the answer, or simply deleting the two smallest digits, ignores the order constraint.

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

Exams that ask this topic