Length of the longest substring with every character appearing even number of times | Function to find length of the longest substring with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required substring ; Traverse the string ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the final answer ; Otherwise ; Return the answer ; Driver Code ; Given string ; : Length of the given string ; Function Call
def lenOfLongestReqSubstr ( s , N ) : NEW_LINE INDENT ind = { } NEW_LINE mask = 0 NEW_LINE ind [ 0 ] = - 1 NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE mask ^= ( 1 << val ) NEW_LINE if ( mask in ind ) : NEW_LINE INDENT ans = max ( ans , i - ind [ mask ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ind [ mask ] = i NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "223015150" NEW_LINE N = len ( s ) NEW_LINE print ( lenOfLongestReqSubstr ( s , N ) ) NEW_LINE DEDENT