human
Give me a code to lexicographic smallest permutation of a String containing the second String as a Substring and the other steps required given the following:
Lexicographic smallest permutation of a String containing the second String as a Substring | Function to print the desired lexicographic smaller string.
Calculate length of the string.
Stores the frequencies of characters of string str1.
Stores the frequencies of characters of string str2.
Decrease the frequency of second string from that of of the first string.
To store the resultant string.
To find the index of first character of the string str2.
Append the characters in lexicographical order.
Append all the current character ( i + ' a ' ).
If we reach first character of string str2 append str2.
Return the resultant string.
Driver code"
Solution in Python:
assistant
#Here is code to lexicographic smallest permutation of a String containing the second String as a Substring and the other steps required.
 def findSmallestString ( str1 , str2 ) :
	freq1 = [ 0 ] * 26
	freq2 = [ 0 ] * 26
	n1 = len ( str1 )
	n2 = len ( str2 )
	for i in range ( n1 ) :
		freq1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1
	for i in range ( n2 ) :
		freq2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1
	for i in range ( 26 ) :
		freq1 [ i ] -= freq2 [ i ]
	res = " "
	minIndex = ord ( str2 [ 0 ] ) - ord ( ' a ' )
	for i in range ( 26 ) :
		for j in range ( freq1 [ i ] ) :
			res += chr ( i + ord ( ' a ' ) )
		if i == minIndex :
			res += str2
	return res
str1 = " geeksforgeeksfor "
str2 = " for "
print ( findSmallestString ( str1 , str2 ) )