Queries to print the character that occurs the maximum number of times in a given range | Function that answers all the queries ; Length of the String ; Number of queries ; Prefix array ; Iterate for all the characters ; Increase the count of the character ; Presum array for all 26 characters ; Update the prefix array ; Answer every query ; Range ; Iterate for all characters ; Times the lowercase character j occurs till r - th index ; Subtract the times it occurred till ( l - 1 ) th index ; Max times it occurs ; Print the answer ; Driver Code
def solveQueries ( Str , query ) : NEW_LINE INDENT ll = len ( Str ) NEW_LINE Q = len ( query ) NEW_LINE pre = [ [ 0 for i in range ( 256 ) ] for i in range ( ll ) ] NEW_LINE for i in range ( ll ) : NEW_LINE INDENT pre [ i ] [ ord ( Str [ i ] ) ] += 1 NEW_LINE if ( i ) : NEW_LINE INDENT for j in range ( 256 ) : NEW_LINE INDENT pre [ i ] [ j ] += pre [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( Q ) : NEW_LINE INDENT l = query [ i ] [ 0 ] NEW_LINE r = query [ i ] [ 1 ] NEW_LINE maxi = 0 NEW_LINE c = ' a ' NEW_LINE for j in range ( 256 ) : NEW_LINE INDENT times = pre [ r ] [ j ] NEW_LINE if ( l ) : NEW_LINE INDENT times -= pre [ l - 1 ] [ j ] NEW_LINE DEDENT if ( times > maxi ) : NEW_LINE INDENT maxi = times NEW_LINE c = chr ( j ) NEW_LINE DEDENT DEDENT print ( " Query ▁ " , i + 1 , " : ▁ " , c ) NEW_LINE DEDENT DEDENT Str = " striver " NEW_LINE query = [ [ 0 , 1 ] , [ 1 , 6 ] , [ 5 , 6 ] ] NEW_LINE solveQueries ( Str , query ) NEW_LINE