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:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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 |
Java
// Java implementation of the approachpublic 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)); }} |
Python3
# Python3 implementation of the approach# Function to return the maximum# length of the sub-string which# contains odd number of 1sdef 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 Codeif __name__ == "__main__": string = "101101" n = len(string) print(maxOddParity(string, n))# This code is contributed by# sanjeev2552 |
C#
// C# implementation of the above approachusing 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 */ |
Javascript
<script>// Javascript implementation of the above approach// Function to return the maximum// length of the sub-string which// contains odd number of 1sfunction maxOddParity(str, n){ // Find the count of 1s in // the given string var cnt = 0; for(var 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 var firstOcc = str.indexOf('1'); var secondOcc = str.indexOf( '1', firstOcc + 1); // Index of the last and the second last // occurrences of '1' in the string var lastOcc = str.lastIndexOf('1'); var 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 codevar str = "101101";var n = str.length;document.write(maxOddParity(str, n));// This code is contributed by bunnyram19</script> |
5


