human
Minimize steps defined by a string required to reach the destination from a given source | Function to find the minimum length of string required to reach from source to destination.
Size of the string.
Stores the index of the four directions E , W , N , S.
If destination reached.
Iterate over the string.
Move east.
Change x1 according to direction E.
Move west.
Change x1 according to direction W.
Move north.
Change y1 according to direction N.
Move south.
Change y1 according to direction S.
Store the max of all positions.
Print the minimum length of string required.
Otherwise , it is impossible.
Given string.
Given source and destination.
Function call"
Solution in Python:
assistant
#Here is code to minimize steps defined by a string required to reach the destination from a given source and the other steps required.
 def minimum_length ( x1 , y1 , x2 , y2 , str ) :
	n = len ( str )
	pos1 = - 1
	pos2 = - 1
	pos3 = - 1
	pos4 = - 1
	if ( x1 == x2 and y1 == y2 ) :
		print ( "0" )
	else :
		for i in range ( n ) :
			if ( x2 > x1 ) :
				if ( str [ i ] == ' E ' ) :
					x1 = x1 + 1
					if ( x1 == x2 ) :
						pos1 = i
			if ( x2 < x1 ) :
				if ( str [ i ] == ' W ' ) :
					x1 = x1 - 1
					if ( x1 == x2 ) :
						pos2 = i
			if ( y2 > y1 ) :
				if ( str [ i ] == ' N ' ) :
					y1 = y1 + 1
					if ( y1 == y2 ) :
						pos3 = i
			if ( y2 < y1 ) :
				if ( str [ i ] == ' S ' ) :
					y1 = y1 - 1
					if ( y1 == y2 ) :
						pos4 = i
		z = 0
		z = max ( pos1 , max ( max ( pos2 , pos3 ) , pos4 ) )
		if ( x1 == x2 and y1 == y2 ) :
			print ( z + 1 )
		else :
			print ( " - 1" )
str = " SESNW "
x1 = 0
x2 = 1
y1 = 0
y2 = 1
minimum_length ( x1 , y1 , x2 , y2 , str )