Bank of America Interview Question
- 0of 0 votes
Magical binary strings are non-empty binary strings if the following two conditions are true:- maulish October 15, 2017 in United States
1. The number of 0's is equal to the number of 1's.
2. For every prefix of the binary string, the number of 1's should not be less than the number of 0's.
A magical string can contain multiple magical substrings. If two consecutive substrings are magical, then we can swap the substrings as long as the resulting string is still a magical string. Given a magical binary string, str, perform zero or more swap operations on its consecutive magical substrings such that the resulting string is as lexicographically large as possible. Two substrings are considered to be consecutive if the last character of the first substring occurs exactly one index before the first character of the second substring.
a single binary string, str.
It is guaranteed that str is a binary string of 1's and 0's only.
1 ≤ length(str) ≤ 50
It is guaranteed that str is a magical string.
Find a string denoting the lexicographically largest magical string that can be formed from str.
Sample Input 0
Explanation of sample
Given the magical string str = 11011000, we can choose two consecutive magical substrings, 1100 and 10, to swap such that the resultant string, str' = 11100100, is the lexicographically largest possible magical string possible. Thus, we return the value of str', which is 11100100, as our answer.
| Report Duplicate | Flag | PURGE
Bank of America
Interview Type: Written Test
Open Chat in New Window