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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum characters to be replaced to make frequency of all characters same
- Missing characters to make a string Pangram
- Make a string from another by deletion and rearrangement of characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count of ungrouped characters after dividing a string into K groups of distinct 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 flips required to generate continuous substrings of 0’s and 1’s
- Minimum number of operations to move all uppercase characters before all lower case characters
- Strings formed from given characters without any consecutive repeating characters
- Print the arranged positions of characters to make palindrome
- Minimum operations to make frequency of all characters equal K
- Minimum characters required to make a password strong
- Minimum characters to be deleted from the end to make given two strings equal