Find numbers of balancing positions in string | Python3 program to find number of balancing points in string ; function to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; Driver Code
MAX_CHAR = 256 NEW_LINE def countBalance ( string ) : NEW_LINE INDENT leftVisited = [ 0 ] * ( MAX_CHAR ) NEW_LINE rightVisited = [ 0 ] * ( MAX_CHAR ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT rightVisited [ ord ( string [ i ] ) ] += 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT leftVisited [ ord ( string [ i ] ) ] += 1 NEW_LINE rightVisited [ ord ( string [ i ] ) ] -= 1 NEW_LINE j = 0 NEW_LINE while j < MAX_CHAR : NEW_LINE INDENT if ( ( leftVisited [ j ] == 0 and rightVisited [ j ] != 0 ) or ( leftVisited [ j ] != 0 and rightVisited [ j ] == 0 ) ) : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if j == MAX_CHAR : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abaababa " NEW_LINE print ( countBalance ( string ) ) NEW_LINE DEDENT