Find max length odd parity substring
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.
Examples:
Input: str = “1001110”
Output: 6
“001110” is the valid sub-string.
Input: str = “101101”
Output: 5
Approach:
- 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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // finds the index of character of string int indexOf(string s, char c, int i) { for(; i < s.length(); i++) if(s[i] == c) return i; return -1; } // finds the last index of character of string int lastIndexOf(string s,char c,int i) { for(; i >= 0; i--) if(s[i] == c) return i; return -1; } // Function to return the maximum // length of the sub-string which // contains odd number of 1s int maxOddParity(string str, int n) { // Find the count of 1s in // the given string int cnt = 0; for (int i = 0; i < n; i++) if (str[i] == '1') cnt++; // If there are only 0s in the string if (cnt == 0) return 0; // If the count of 1s is odd then // the complete string has odd parity if (cnt % 2 == 1) return n; // Index of the first and the second // occurrences of '1' in the string int firstOcc = indexOf(str,'1',0); int secondOcc = indexOf(str,'1', firstOcc + 1); // Index of the last and the second last // occurrences of '1' in the string int lastOcc = lastIndexOf(str,'1',str.length()-1); int secondLastOcc = lastIndexOf(str,'1', lastOcc - 1); // Result will the sub-string ending just // before the last occurrence of '1' or the // sub-string starting just after the first // occurrence of '1' // choose the one with the maximum length return max(lastOcc, n - firstOcc - 1); } // Driver code int main() { string str = "101101"; int n = str.length(); cout<<(maxOddParity(str, n)); } // This code is contributed by Arnab Kundu |
chevron_right
filter_none
Java
// Java implementation of the approach public class GFG { // Function to return the maximum // length of the sub-string which // contains odd number of 1s static int maxOddParity(String str, int n) { // Find the count of 1s in // the given string int cnt = 0; for (int i = 0; i < n; i++) if (str.charAt(i) == '1') cnt++; // If there are only 0s in the string if (cnt == 0) return 0; // If the count of 1s is odd then // the complete string has odd parity if (cnt % 2 == 1) return n; // Index of the first and the second // occurrences of '1' in the string int firstOcc = str.indexOf('1'); int secondOcc = str.indexOf('1', firstOcc + 1); // Index of the last and the second last // occurrences of '1' in the string int lastOcc = str.lastIndexOf('1'); int secondLastOcc = str.lastIndexOf('1', lastOcc - 1); // Result will the sub-string ending just // before the last occurrence of '1' or the // sub-string starting just after the first // occurrence of '1' // choose the one with the maximum length return Math.max(lastOcc, n - firstOcc - 1); } // Driver code public static void main(String[] args) { String str = "101101"; int n = str.length(); System.out.print(maxOddParity(str, n)); } } |
chevron_right
filter_none
Python3
# Python3 implementation of the approach # Function to return the maximum # length of the sub-string which # contains odd number of 1s def maxOddParity(string, n): # Find the count of 1s in # the given string cnt = 0 for i in range(n): if string[i] != '1': cnt += 1 # If there are only 0s in the string if cnt == 0: return 0 # If the count of 1s is odd then # the complete string has odd parity if cnt % 2 == 1: return n # Index of the first and the second # occurrences of '1' in the string firstOcc = string.index('1') secondOcc = string.index('1', firstOcc + 1) # Index of the last and the second last # occurrences of '1' in the string lastOcc = string.rindex('1') secondLastOcc = string.rindex('1', 0, lastOcc) # Result will the sub-string ending just # before the last occurrence of '1' or the # sub-string starting just after the first # occurrence of '1' # choose the one with the maximum length return max(lastOcc, n - firstOcc - 1) # Driver Code if __name__ == "__main__": string = "101101" n = len(string) print(maxOddParity(string, n)) # This code is contributed by # sanjeev2552 |
chevron_right
filter_none
C#
// C# implementation of the above approach using System; class GFG { // Function to return the maximum // length of the sub-string which // contains odd number of 1s static int maxOddParity(String str, int n) { // Find the count of 1s in // the given string int cnt = 0; for (int i = 0; i < n; i++) if (str[i] == '1') cnt++; // If there are only 0s in the string if (cnt == 0) return 0; // If the count of 1s is odd then // the complete string has odd parity if (cnt % 2 == 1) return n; // Index of the first and the second // occurrences of '1' in the string int firstOcc = str.IndexOf('1'); int secondOcc = str.IndexOf('1', firstOcc + 1); // Index of the last and the second last // occurrences of '1' in the string int lastOcc = str.LastIndexOf('1'); int secondLastOcc = str.LastIndexOf('1', lastOcc - 1); // Result will the sub-string ending just // before the last occurrence of '1' or the // sub-string starting just after the first // occurrence of '1' // choose the one with the maximum length return Math.Max(lastOcc, n - firstOcc - 1); } // Driver code public static void Main(String[] args) { String str = "101101"; int n = str.Length; Console.WriteLine(maxOddParity(str, n)); } } /* This code contributed by PrinciRaj1992 */ |
chevron_right
filter_none
Output:
5
Recommended Posts:
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- 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
- Length of the longest substring that do not contain any palindrome
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring with equal 1s and 0s
- Maximum length substring having all same characters after k changes
- Length of the longest valid substring
- Minimum K such that every substring of length atleast K contains a character c
- Substring with highest frequency length product
- Convert to a string that is repetition of a substring of k length
- Length of the longest substring without repeating characters
- Minimum length substring with exactly K distinct characters
- Length of the longest substring with no consecutive same letters
- Check if string can be rearranged so that every Odd length Substring is Palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


