human
Lexicographically smallest permutation of a string that contains all substrings of another string | Function to reorder the B to contain all the substrings of A.
Find length of strings.
Initialize array to count the frequencies of the character.
Counting frequencies of character in B.
Find remaining character in B.
Declare the reordered string.
Loop until freq [ j ] > 0.
Decrement the value from freq array.
Check if A [ j ] > A [ 0 ].
Check if A [ j ] < A [ 0 ].
Append the remaining characters to the end of the result.
Push all the values from frequency array in the answer.
Return the answer.
Driver Code.
Given strings A and B.
Function call"
How can the above be solved in Python?
 ... What is a code to lexicographically smallest permutation of a string that contains all substrings of another string and the other steps required?
assistant
#Here is code to lexicographically smallest permutation of a string that contains all substrings of another string and the other steps required.
 def reorderString ( A , B ) :
	size_a = len ( A )
	size_b = len ( B )
	freq = [ 0 ] * 300
	for i in range ( size_b ) :
		freq [ ord ( B [ i ] ) ] += 1
	for i in range ( size_a ) :
		freq [ ord ( A [ i ] ) ] -= 1
	for j in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) :
		if ( freq [ j ] < 0 ) :
			return " - 1"
	answer = [ ]
	for j in range ( ord ( ' a ' ) , ord ( A [ 0 ] ) ) :
		while ( freq [ j ] > 0 ) :
			answer . append ( j )
			freq [ j ] -= 1
	first = A [ 0 ]
	for j in range ( size_a ) :
		if ( A [ j ] > A [ 0 ] ) :
			break
		if ( A [ j ] < A [ 0 ] ) :
			answer += A
			A = " "
			break
	while ( freq [ ord ( first ) ] > 0 ) :
		answer . append ( first )
		freq [ ord ( first ) ] -= 1
	answer += A
	for j in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) :
		while ( freq [ j ] ) :
			answer . append ( chr ( j ) )
			freq [ j ] -= 1
	return " " . join ( answer )
if __name__ == ' _ _ main _ _ ' :
	A = " aa "
	B = " ababab "
	print ( reorderString ( A , B ) )