Profile photo for Dinesh Khandelwal

Rough Algorithm :

  1. Take the permutation 1 2 3 .... n. For example if n=5 take permutation 1 2 3 4 5. Call It original sequence.
  2. Mark all the continuous occurrences of D in the given signature.
  3. Then for each continuous sequence of D, going from left to right reverse the strip corresponding to that in original sequence.Every time make the new sequence as original sequence.

Example :

  1. Take the above case. Signature = DDIIDI.
  2. Take original permutation as 1 2 3 4 5 6 7.
  3. Then for first continuous sequence DD reverse the strip 1 2 3 to 3 2 1 hence sequence becomes 3 2 1 4 5 6 7.
  4. Then for second sequence D reverse strip 5 6 to 6 5 hence sequence becomes 3 2 1 4 6 5 7.
  5. There is no continuous D left we are done and reached the answer.

Source : Lexicographically Smallest permutation given a signature

View 2 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025