human
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"
How can the above be solved in Python?
assistant
#Here is code to queries to print the character that occurs the maximum number of times in a given range and the other steps required.
 def solveQueries ( Str , query ) :
	ll = len ( Str )
	Q = len ( query )
	pre = [ [ 0 for i in range ( 256 ) ] for i in range ( ll ) ]
	for i in range ( ll ) :
		pre [ i ] [ ord ( Str [ i ] ) ] += 1
		if ( i ) :
			for j in range ( 256 ) :
				pre [ i ] [ j ] += pre [ i - 1 ] [ j ]
	for i in range ( Q ) :
		l = query [ i ] [ 0 ]
		r = query [ i ] [ 1 ]
		maxi = 0
		c = ' a '
		for j in range ( 256 ) :
			times = pre [ r ] [ j ]
			if ( l ) :
				times -= pre [ l - 1 ] [ j ]
			if ( times > maxi ) :
				maxi = times
				c = chr ( j )
		print ( " Query ▁ " , i + 1 , " : ▁ " , c )
Str = " striver "
query = [ [ 0 , 1 ] , [ 1 , 6 ] , [ 5 , 6 ] ]
solveQueries ( Str , query )