Given a binary string S which only contains '0' and '1' such that the '0' or '1' at the 1st position is just next to '0' or '1' at the last position i.e. string is cyclic. Your task is to re arrange the string such that all the '1' in the string are next to each other (Note : the string is cyclic ).
You can choose any '1' in the string and swap its position with the first '1' in any of the two direction.
Find the minimum number of moves in which the task can be done.
Input: S = "010110101" Output: 3 Explanation: In the first move 010110101 is changed to 010110110.In the second move 010110110 is changed to 010111100. In the third move 010111100 is change to 001111100.
Input: S = "111" Output: 0 Explanation: All one's are clustred. So answer will be zero.
You don't need to read or print anything. Your task is to complete the function minimumMoves() which takes string S as input parameter and returns the minimum number of moves.
Expected Time Complexity: O(|S|)
Expected Space Complexity: O(1)
1 ≤ |S| ≤ 100000
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?Yes