Leaderboard
Showing:
Cluster all the 1 in a given string
Easy Accuracy: 0.0% Submissions: 0 Points: 2

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.

 

Example 1:

Input: S = "010110101"
Output: 3
Explanation: In the first move 01011010 
is changed to 010110110.In the second move 
010110110 is changed to 010111100. In the 
third move 010111100 is change to 001111100.

Example 2:

Input: S = "111"
Output: 0
Explanation: All one's are clustred. So answer will
be zero.

 

Your Task:

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)

 

Constraints:
1 ≤ |S| ≤ 100000

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Cluster all the 1 in a given string

Output Window