Given a string consisting only of 1’s and 0’s. In one flip we can change any continuous sequence of this string. Find this minimum number of flips so the string consist of same characters only.
Input : 00011110001110 Output : 2 We need to convert 1's sequence so string consist of all 0's. Input : 010101100011 Output : 4
We need to find the min flips in string so all characters are equal. All we have to find numbers of sequence which consisting of 0’s or 1’s only. Then number of flips required will be half of this number as we can change all 0’s or all 1’s.
Time Complexity: O(n)
This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Replace minimal number of characters to make all characters pair wise distinct
- Make a string from another by deletion and rearrangement of characters
- Missing characters to make a string Pangram
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimum characters to be added at front to make string palindrome
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum number of characters to be removed to make a binary string alternate
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- String with k distinct characters and no same characters adjacent
- Number of flips to make binary string alternate | Set 1
- Minimum number of operations to move all uppercase characters before all lower case characters
- Print the arranged positions of characters to make palindrome
- Minimum operations to make frequency of all characters equal K
- Make lexicographically smallest palindrome by substituting missing characters