Given a numerical string S of length N, the task is to find the length of the longest substring of S where each element occurs even number of times.
Examples:
Input: S = “324425”
Output: 4
Explanation: Two substrings consisting of even frequent elements only are “44” and “2442”. Since “2442” is the longer of the two, print 4 as the required answer.Input: S = “223015150”
Output: 6
Explanation: Three substrings consisting of even frequent elements only are “22”, “1515” and “015150”. Since “015150” is the longest among the three, print 4 as the required answer.
Naive Approach: The simplest approach is to generate all possible substrings of even length from the given string and for each substring, check if it contains characters with even frequencies only or not. Print the length of the longest of all such substrings.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach the idea is to use Bit Masking. Follow the steps below to solve the problem:
- Traverse the string from left to right.
- While traversing, use a bitmask variable, say mask, to keep track of the occurrence of each element from index 0 to the current index, whether it is even or odd. The ith (0 ≤ i ≤ 9) bit in the mask is 0 if the digit i has occurred even number of times up to the current index, and 1 if it has occurred an odd number of times.
- Use a variable val to store the value of digit present at the current index.
- To update the occurrence of val, take Bitwise XOR of mask with 1 << val.
- Use a Hash table ind to keep track of the index of each bitmask.
- After updating the value of mask at the current index, check if it is present in ind or not:
- If the value of the mask is already present in ind, it means that each element from index ind[mask] + 1 to current index occurs even number of times. Therefore, update the answer if the length of this segment from index ind[mask] + 1 to the current index is greater than the answer.
- Otherwise, assign the value of the current index to ind[mask].
- Finally, print the length of the required substring after completing the above steps.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find length of the // longest substring with each element // occurring even number of times int lenOfLongestReqSubstr(string s, int N) { // Initialize unordered_map unordered_map< int , int > ind; int mask = 0; ind[0] = -1; // Stores the length of the // longest required substring int ans = 0; // Traverse the string for ( int i = 0; i < N; i++) { // Stores the value of the // digit present at current index int val = s[i] - '0' ; // Bitwise XOR of the mask with // 1 left-shifted by val mask ^= (1 << val); // Check if the value of mask is // already present in ind or not if (ind.find(mask) != ind.end()) { // Update the final answer ans = max(ans, i - ind[mask]); } // Otherwise else ind[mask] = i; } // Return the answer return ans; } // Driver Code int main() { // Given string string s = "223015150" ; // Length of the given string int N = s.length(); // Function Call cout << lenOfLongestReqSubstr(s, N); return 0; } |
Java
// Java program for the // above approach import java.util.*; class GFG{ // Function to find length of the // longest subString with each element // occurring even number of times static int lenOfLongestReqSubstr(String s, int N) { // Initialize unordered_map HashMap<Integer, Integer> ind = new HashMap<>(); int mask = 0 ; ind.put( 0 , - 1 ); // Stores the length of the // longest required subString int ans = 0 ; // Traverse the String for ( int i = 0 ; i < N; i++) { // Stores the value of the // digit present at current // index int val = s.charAt(i) - '0' ; // Bitwise XOR of the mask // with 1 left-shifted by val mask ^= ( 1 << val); // Check if the value of mask is // already present in ind or not if (ind.containsKey(mask)) { // Update the final answer ans = Math.max(ans, i - ind.get(mask)); } // Otherwise else ind.put(mask, i); } // Return the answer return ans; } // Driver Code public static void main(String[] args) { // Given String String s = "223015150" ; // Length of the given String int N = s.length(); // Function Call System.out.print( lenOfLongestReqSubstr(s, N)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program for the # above approach # Function to find length of # the longest substring with # each element occurring even # number of times def lenOfLongestReqSubstr(s, N): # Initialize unordered_map ind = {} mask = 0 ind[ 0 ] = - 1 # Stores the length of the # longest required substring ans = 0 # Traverse the string for i in range (N): # Stores the value of the # digit present at current # index val = ord (s[i]) - ord ( '0' ) # Bitwise XOR of the mask # with 1 left-shifted by val mask ^ = ( 1 << val) # Check if the value of mask # is already present in ind # or not if (mask in ind): # Update the final answer ans = max (ans, i - ind[mask]) # Otherwise else : ind[mask] = i # Return the answer return ans # Driver Code if __name__ = = "__main__" : # Given string s = "223015150" #:Length of the given # string N = len (s) # Function Call print (lenOfLongestReqSubstr(s, N)) # This code is contributed by Chitranayal |
C#
// C# program for the // above approach using System; using System.Collections.Generic; class GFG{ // Function to find length of the // longest subString with each element // occurring even number of times static int lenOflongestReqSubstr(String s, int N) { // Initialize unordered_map Dictionary< int , int > ind = new Dictionary< int , int >(); int mask = 0; ind.Add(0, -1); // Stores the length of the // longest required subString int ans = 0; // Traverse the String for ( int i = 0; i < N; i++) { // Stores the value of the // digit present at current // index int val = s[i] - '0' ; // Bitwise XOR of the mask // with 1 left-shifted by val mask ^= (1 << val); // Check if the value of mask is // already present in ind or not if (ind.ContainsKey(mask)) { // Update the readonly answer ans = Math.Max(ans, i - ind[mask]); } // Otherwise else ind.Add(mask, i); } // Return the answer return ans; } // Driver Code public static void Main(String[] args) { // Given String String s = "223015150" ; // Length of the given String int N = s.Length; // Function Call Console.Write( lenOflongestReqSubstr(s, N)); } } // This code is contributed by 29AjayKumar |
6
Time Complexity: O(N)
Auxiliary Space: O(N)
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.