human
Can you provide a code to length of longest substring to be deleted to make a string equal to another string and the other steps required given the following:
Length of longest substring to be deleted to make a string equal to another string | Function to prthe length of longest substring to be deleted.
Stores the length of string.
Store the position of previous matched character of str1.
Store the position of first occurrence of str2 in str1.
Find the position of the first occurrence of str2.
Store the index of str1.
If both characters not matched.
Store the length of the longest deleted substring.
Store the position of last occurrence of str2 in str1.
If both characters not matched.
Update res.
Update res ..
Given string.
Function call"
How can the above be solved in Python?
assistant
#Here is code to length of longest substring to be deleted to make a string equal to another string and the other steps required.
 def longDelSub ( str1 , str2 ) :
	N = len ( str1 )
	M = len ( str2 )
	prev_pos = 0
	pos = [ 0 ] * M
	for i in range ( M ) :
		index = prev_pos
		while ( index < N and str1 [ index ] != str2 [ i ] ) :
			index += 1
		pos [ i ] = index
		prev_pos = index + 1
	res = N - prev_pos
	prev_pos = N - 1
	for i in range ( M - 1 , - 1 , - 1 ) :
		index = prev_pos
		while ( index >= 0 and str1 [ index ] != str2 [ i ] ) :
			index -= 1
		if ( i != 0 ) :
			res = max ( res , index - pos [ i - 1 ] - 1 )
		prev_pos = index - 1
	res = max ( res , prev_pos + 1 )
	return res
str1 = " GeeksforGeeks "
str2 = " forks "
print ( longDelSub ( str1 , str2 ) )