Queries to find Kth greatest character in a range [ L , R ] from a string with updates | Python3 Program to implement the above approach Function to find the kth greatest character from the string ; Sorting the in non - increasing Order ; Function to prthe K - th character from the subS [ l ] . . S [ r ] ; 0 - based indexing ; Subof strr from the indices l to r . ; Extract kth Largest character ; Function to replace character at pos of strr by the character s ; Index of S to be updated . ; Character to be replaced at index in S ; Driver Code ; Given string ; Count of queries ; Queries
def find_kth_largest ( strr , k ) : NEW_LINE INDENT strr = sorted ( strr ) NEW_LINE strr = strr [ : : - 1 ] NEW_LINE return strr [ k - 1 ] NEW_LINE DEDENT def printCharacter ( strr , l , r , k ) : NEW_LINE INDENT l = l - 1 NEW_LINE r = r - 1 NEW_LINE temp = strr [ l : r - l + 1 ] NEW_LINE ans = find_kth_largest ( temp , k ) NEW_LINE return ans NEW_LINE DEDENT def updateString ( strr , pos , s ) : NEW_LINE INDENT index = pos - 1 NEW_LINE c = s NEW_LINE strr [ index ] = c NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " abcddef " NEW_LINE strr = [ i for i in strr ] NEW_LINE Q = 3 NEW_LINE print ( printCharacter ( strr , 1 , 2 , 2 ) ) NEW_LINE updateString ( strr , 4 , ' g ' ) NEW_LINE print ( printCharacter ( strr , 1 , 5 , 4 ) ) NEW_LINE DEDENT