human
Largest index for each distinct character in given string with frequency K | Python3 implementation of the approach Function to find largest index for each distinct character occuring exactly K times ..
Function to find largest index for each distinct character occuring exactly K times.
Finding all characters present in S.
Finding all distinct characters in S.
To store result for each character.
Loop through each lower case English character.
If current character is absent in s.
Getting current character.
Finding count of character ch in S.
To store max Index encountred so far.
Printing required result.
If no such character exists , print - 1.
Driver code"
Solution in Python:

What is a code to largest index for each distinct character in given string with frequency K and the other steps required?
assistant
#Here is code to largest index for each distinct character in given string with frequency K and the other steps required.
 def maxSubstring ( S , K , N ) :
	def maxSubstring ( S , K , N ) :
		freq = [ 0 for i in range ( 26 ) ]
		for i in range ( N ) :
			freq [ ord ( S [ i ] ) - 97 ] = 1
		answer = [ ]
		for i in range ( 26 ) :
			if ( freq [ i ] == 0 ) :
				continue
			ch = chr ( i + 97 )
			count = 0
			index = - 1
			for j in range ( N ) :
				if ( S [ j ] == ch ) :
					count += 1
				if ( count == K ) :
					index = j
			answer . append ( [ ch , index ] )
		flag = 0
		for i in range ( len ( answer ) ) :
			if ( answer [ i ] [ 1 ] > - 1 ) :
				flag = 1
				print ( answer [ i ] [ 0 ] , answer [ i ] [ 1 ] )
		if ( flag == 0 ) :
			print ( " - 1" )
if __name__ == ' _ _ main _ _ ' :
	S = " cbaabaacbcd "
	K = 2
	N = len ( S )
	maxSubstring ( S , K , N )