human
Lexicographic rank of a string among all its substrings | Function to find lexicographic rank of among all its substring.
Length of string.
Traverse the given string and store the indices of each character.
Extract the index.
Store it in the vector.
Traverse the alphaIndex array lesser than the index of first character of given string.
If alphaIndex [ i ] size exceeds 0.
Traverse over the indices.
Add count of substring equal to n - alphaIndex [ i ] [ j ].
Store all substrings in a vector strr starting with the first character of the given string.
Insert the current character to substring.
Store the subformed.
Sort the substring in the lexicographical order.
Find the rank of given string.
Increase the rank until the given is same.
If substring is same as the given string.
Add 1 to rank of the given string.
Driver Code.
Given string.
Function call"
How can the above be solved in Python?
 Write me a code to lexicographic rank of a string among all its substrings and the other steps required?
assistant
#Here is code to lexicographic rank of a string among all its substrings and the other steps required.
 def lexicographicRank ( s ) :
	n = len ( s )
	alphaIndex = [ [ ] for i in range ( 26 ) ]
	for i in range ( len ( s ) ) :
		x = ord ( s [ i ] ) - ord ( ' a ' )
		alphaIndex [ x ] . append ( i )
	rank = - 1
	for i in range ( 26 ) :
		if ord ( ' a ' ) + i >= ord ( s [ 0 ] ) :
			break
		if len ( alphaIndex [ i ] ) > 0 :
			for j in range ( len ( alphaIndex [ i ] ) ) :
				rank = rank + ( n - alphaIndex [ i ] [ j ] )
	strr = [ ]
	for i in range ( len ( alphaIndex [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] ) ) :
		substring = [ ]
		jj = alphaIndex [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] [ i ]
		for j in range ( jj , n ) :
			substring . append ( s [ j ] )
			strr . append ( substring )
	strr = sorted ( strr )
	for i in range ( len ( strr ) ) :
		if ( strr [ i ] != s ) :
			rank += 1
		else :
			break
	return rank + 1
if __name__ == ' _ _ main _ _ ' :
	strr = " enren "
	print ( lexicographicRank ( strr ) )