human
Write me a code to lexicographically smallest and largest anagrams of a string containing another string as its substring and the other steps required given the following:
Lexicographically smallest and largest anagrams of a string containing another string as its substring | Function to find the lexicographically smallest anagram of string which contains another string.
Initializing the dictionary and set.
Iterating over s1.
Storing the frequency of characters present in s1.
Storing the distinct characters present in s1.
Decreasing the frequency of characters from M that are already present in s2.
Traversing alphabets in sorted order.
If current character of set is not equal to current character of s2.
If element is equal to current character of s2.
Checking for second distinct character in s2.
s2 [ j ] will store second distinct character.
Return the answer.
Function to find the lexicographically largest anagram of string which contains another string.
Getting the lexicographically smallest anagram.
d1 stores the prefix.
d2 stores the suffix.
Return the result.
Given two strings.
Function Calls"
How can the above be solved in Python?
assistant
#Here is code to lexicographically smallest and largest anagrams of a string containing another string as its substring and the other steps required.
 def lexico_smallest ( s1 , s2 ) :
	M = { }
	S = [ ]
	pr = { }
	for i in range ( len ( s1 ) ) :
		if s1 [ i ] not in M :
			M [ s1 [ i ] ] = 1
		else :
			M [ s1 [ i ] ] += 1
		S . append ( s1 [ i ] )
	S = list ( set ( S ) )
	S . sort ( )
	for i in range ( len ( s2 ) ) :
		if s2 [ i ] in M :
			M [ s2 [ i ] ] -= 1
	c = s2 [ 0 ]
	index = 0
	res = " "
	for x in S :
		if ( x != c ) :
			for i in range ( 1 , M [ x ] + 1 ) :
				res += x
		else :
			j = 0
			index = len ( res )
			while ( s2 [ j ] == x ) :
				j += 1
			if ( s2 [ j ] < c ) :
				res += s2
				for i in range ( 1 , M [ x ] + 1 ) :
					res += x
			else :
				for i in range ( 1 , M [ x ] + 1 ) :
					res += x
				index += M [ x ]
				res += s2
	pr [ res ] = index
	return pr
def lexico_largest ( s1 , s2 ) :
	Pr = dict ( lexico_smallest ( s1 , s2 ) )
	d1 = " "
	key = [ * Pr ] [ 0 ]
	for i in range ( Pr . get ( key ) - 1 , - 1 , - 1 ) :
		d1 += key [ i ]
	d2 = " "
	for i in range ( len ( key ) - 1 , Pr [ key ] + len ( s2 ) - 1 , - 1 ) :
		d2 += key [ i ]
	res = d2 + s2 + d1
	return res
s1 = " ethgakagmenpgs "
s2 = " geeks "
print ( * lexico_smallest ( s1 , s2 ) )
print ( lexico_largest ( s1 , s2 ) )