Problem

Lexicographically Maximum Final Sequence

Learn this problem
AmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

You are given a binary string shipmentData consisting only of '0' and '1'.

A final string is built from a chosen ordering of shipmentData as follows:

  1. Start with an empty string finalSequence.
  2. For each character c in the chosen ordering from left to right, append c to finalSequence, then reverse finalSequence.

You may rearrange the characters of shipmentData arbitrarily before applying the operation. Return the rearranged shipmentData string that should be fed into the operation to produce the lexicographically maximum possible finalSequence.

Do not return finalSequence itself. The answer must be a rearrangement of the original shipmentData.

Function

rearrangeShipmentData(shipmentData: String) → String

Examples

Example 1

shipmentData = "0011"return = "0101"

The returned rearranged string is "0101". If this string is fed into the operation, the resulting final sequence is "1100", which is lexicographically maximum among all rearrangements.

Example 2

shipmentData = "10100"return = "00101"

The final sequence reads positions 5,3,1,2,4 from the rearranged string. Placing the two '1' characters at positions 5 and 3 produces final sequence "11000".

Constraints

  • shipmentData.length >= 1
  • shipmentData contains only '0' and '1'.

More Amazon problems

drafts saved locally
public String rearrangeShipmentData(String shipmentData) {
  // write your code here
}
shipmentData"0011"
expected"0101"
checking account