Developers at Amazon are creating a single sign-on application for the web apps in AWS. In one of its modules, a security code is in the form of a binary string that changes every minute. The characters at a pair of indices i and i+1 are swapped such that the resulting code is the lexicographically maximum binary string that can be achieved in this step. If it is not possible to make the code lexicographically greater, then the code does not change.
String x is lexicographically greater than string y if either y is a prefix of x (and x ≠ y), or there any i exists such that (0 ≤ i < min{|x|, |y|}), that yi < xi, and for any j (0 ≤ j < i) xj = yj. Here, |a| denotes the length of the string a.
Given the string initialCode, the initial security code, and an integer minutesElapsed, find the security code after minutesElapsed minutes.
initialCode = "00100101" minutesElapsed = 4
The number of times the code changes is minutesElapsed = 4.
After 1 minute, the characters at the second and the third positions are swapped and the code becomes code = "01000101".
After 2 minutes, the characters at the first and the second positions are swapped and the code becomes code = "10000101".
After 3 minutes, the characters at the fifth and the sixth positions are swapped and the code becomes code = "10001001".
After 4 minutes, the characters at the fourth and the fifth positions are swapped and the code becomes code = "10010001".
The security code after 4 minutes is "10010001", hence we return "10010001" as the answer.