human
Check if a string can be obtained by rotating another string d places | Function to reverse an array from left index to right index ( both inclusive ).
Function that returns true if str1 can be made equal to str2 by rotating either d places to the left or to the right.
Left Rotation string will contain the string rotated Anti - Clockwise Right Rotation string will contain the string rotated Clockwise.
Copying the str1 string to left rotation string and right rotation string.
Rotating the string d positions to the left.
Rotating the string d positions to the right.
Compairing the rotated strings.
If cannot be made equal with left rotation.
If cannot be made equal with right rotation.
If both or any one of the rotations of str1 were equal to str2.
Driver code.
d is the rotating factor.
In case length of str1 < d"
Solution in Python:
assistant
#Here is code to check if a string can be obtained by rotating another string d places and the other steps required.
 def ReverseArray ( arr , left , right ) :
	while ( left < right ) :
		temp = arr [ left ] ;
		arr [ left ] = arr [ right ] ;
		arr [ right ] = temp ;
		left += 1 ;
		right -= 1 ;
def RotateAndCheck ( str1 , str2 , d ) :
	if ( len ( str1 ) != len ( str2 ) ) :
		return False ;
	left_rot_str1 = [ ] ; right_rot_str1 = [ ] ;
	left_flag = True ; right_flag = True ;
	str1_size = len ( str1 ) ;
	for i in range ( str1_size ) :
		left_rot_str1 . append ( str1 [ i ] ) ;
		right_rot_str1 . append ( str1 [ i ] ) ;
	ReverseArray ( left_rot_str1 , 0 , d - 1 ) ;
	ReverseArray ( left_rot_str1 , d , str1_size - 1 ) ;
	ReverseArray ( left_rot_str1 , 0 , str1_size - 1 ) ;
	ReverseArray ( right_rot_str1 , 0 , str1_size - d - 1 ) ;
	ReverseArray ( right_rot_str1 , str1_size - d , str1_size - 1 ) ;
	ReverseArray ( right_rot_str1 , 0 , str1_size - 1 ) ;
	for i in range ( str1_size ) :
		if ( left_rot_str1 [ i ] != str2 [ i ] ) :
			left_flag = False ;
		if ( right_rot_str1 [ i ] != str2 [ i ] ) :
			right_flag = False ;
	if ( left_flag or right_flag ) :
		return True ;
	return False ;
if __name__ == " _ _ main _ _ " :
	str1 = list ( " abcdefg " ) ;
	str2 = list ( " cdefgab " ) ;
	d = 2 ;
	d = d % len ( str1 ) ;
	if ( RotateAndCheck ( str1 , str2 , d ) ) :
		print ( " Yes " ) ;
	else :
		print ( " No " ) ;