Given a binary string str, the task is to find the maximum length of the sub-string of str that has odd parity. A binary string is said be odd parity if it contains odd number of 1s.
Input: str = “1001110”
“001110” is the valid sub-string.
Input: str = “101101”
- Count the number of 1s in the given string and store it in a variable cnt.
- If cnt = 0 then there is no sub-string possible with odd parity so the result will be 0.
- If cnt is odd then the result will be the complete string.
- Now for the case when cnt is even and > 0, the required sub-string will either start at index 0 and end just before the last occurrence of 1 or start just after the first occurrence of 1 and end at the end of the given string.
- Choose the one with the greater length among the two sub-strings in the previous step.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find length of longest substring with at most K normal characters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find length of longest subsequence of one string which is substring of another string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Maximum length substring having all same characters after k changes
- Length of the longest substring that do not contain any palindrome
- Length of the smallest substring which contains all vowels
- Length of longest substring having all characters as K
- Length of the longest substring with equal 1s and 0s
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest valid substring
- Minimum K such that every substring of length atleast K contains a character c
- Minimum length substring with exactly K distinct characters
- Length of the longest substring without repeating characters
- Number of binary strings such that there is no substring of length ≥ 3
- Substring with highest frequency length product
- Convert to a string that is repetition of a substring of k length
- Length of the longest substring with no consecutive same letters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.