Given two strings a and b. The task is to find if the string 'b' can be obtained by rotating another string 'a' by exactly 2 places.
Example 1:
Input:
a = amazon
b = azonam
Output: 1
Explanation: amazon can be rotated anti
clockwise by two places, which will make
it as azonam.
Example 2:
Input:
a = geeksforgeeks
b = geeksgeeksfor
Output: 0
Explanation: If we rotate geeksforgeeks by
two place in any direction , we won't get
geeksgeeksfor.
Your Task:
The task is to complete the function isRotated() which takes two strings as input parameters and checks if given strings can be formed by rotations. The function returns true if string 1 can be obtained by rotating string 2 by two places, else it returns false.
Expected Time Complexity: O(N).
Expected Auxilary Complexity: O(N).
Challenge: Try doing it in O(1) space complexity.
Constraints:
1 ≤ length of a, b ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3


class Solution:
    #Function to check if a string can be obtained by rotating
    #another string by exactly 2 places.
    def isRotated(self,str1,str2):
        #code here

Solutions:
class Solution:

	def isRotated(self, str1, str2):
		if str1[2:] + str1[:2] == str2 or str1[len(str1) - 2:] + str1[:len(str1) - 2] == str2:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return 0
		crt = ''
		acrt = ''
		acrt = str2[len(str2) - 2:] + str2[0:len(str2) - 2]
		crt = str2[2:] + str2[0:2]
		return crt == str1 or acrt == str1


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		k = len(str2)
		if n != k:
			return False
		if str1[0] == str2[n - 2] and str1[1] == str2[n - 1]:
			for i in range(2, n):
				if str1[i] != str2[i - 2]:
					return False
		elif str1[n - 2] == str2[0] and str1[n - 1] == str2[1]:
			for i in range(2, n):
				if str2[i] != str1[i - 2]:
					return False
		else:
			return False
		return True


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		k = len(str2)
		if n != k:
			return False
		rotateclock = True
		rotateanticlock = True
		for i in range(n):
			if str1[i] != str2[(i + 2) % n]:
				rotateclock = False
		for i in range(k):
			if str1[(i + 2) % n] != str2[i]:
				rotateanticlock = False
		return rotateclock or rotateanticlock


class Solution:

	def isRotated(self, str1, str2):
		if str1[2:] + str1[:2] == str2:
			return True
		elif str1[len(str1) - 2:] + str1[:len(str1) - 2] == str2:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		s1 = str1[2:] + str1[0:2]
		s2 = str1[-2:] + str1[0:-2]
		if s1 == str2 or s2 == str2:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		st = str2[0:2]
		st1 = str2[2:]
		left_result = st1 + st
		st2 = str2[-2:]
		st3 = str2[:-2]
		right_result = st2 + st3
		if left_result == str1 or right_result == str1:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		(clockwise, anticlockwise) = (True, True)
		for i in range(n):
			if str1[i] != str2[(i + 2) % n]:
				clockwise = False
				break
		for i in range(n):
			if str1[(i + 2) % n] != str2[i]:
				anticlockwise = False
				break
		return clockwise or anticlockwise


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		m = len(str2)
		if n != m:
			return False
		if n < 2:
			return str1 == str2
		clk = ''
		anti_clk = ''
		clk = clk + str1[2:] + str1[0:2]
		anti_clk = anti_clk + str1[n - 2:] + str1[0:n - 2]
		return clk == str2 or anti_clk == str2


class Solution:

	def isRotated(self, str1, str2):
		if str1[2:] == str2[:-2] and str1[:2] == str2[-2:] or (str1[-2:] == str2[:2] and str1[:-2] == str2[2:]):
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		s1 = str1[2:] + str1[:2]
		s2 = str2[2:] + str2[:2]
		if str1 == s2 or str2 == s1:
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		temp1 = str1[n - 2:] + str1[0:n - 2]
		temp2 = str2[n - 2:] + str2[0:n - 2]
		if temp1 == str2 or temp2 == str1:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return 0
		output = 0
		n = len(str1)
		flag1 = True
		flag2 = True
		for i in range(len(str1)):
			if not str1[i] == str2[(i + 2) % n]:
				flag1 = False
			if not str1[i] == str2[(n - 2 + i) % n]:
				flag2 = False
			if flag1 and flag2:
				break
		return 1 if flag1 or flag2 else 0


class Solution:

	def isRotated(self, str1, str2):
		i = 0
		clk = str2[2:] + str2[0:2]
		antclk = str2[len(str2) - 2] + str2[len(str2) - 1] + str2[0:len(str2) - 2]
		if str1 == clk or str1 == antclk:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		temp1 = str1[n - 2:] + str1[0:n - 2]
		temp2 = str2[n - 2:] + str2[0:n - 2]
		if temp1 == str2 or temp2 == str1:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		return str1 == str2[2:] + str2[:2] or str1 == str2[-2:] + str2[:-2]


class Solution:

	def isRotated(self, str1, str2):
		str4 = str1[-2:] + str1[:-2]
		str3 = str1[2:] + str1[:2]
		if str3 == str2 or str4 == str2:
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		crotate = str1[2:] + str1[0:2]
		acrotate = str1[len(str1) - 2:] + str1[0:len(str1) - 2]
		if str2 == crotate or str2 == acrotate:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		s = str1[0:2]
		k = str1[2:] + s
		if k == str2:
			return 1
		else:
			s = str1[-2:]
			n = len(str1)
			k = s + str1[:n - 2]
			if k == str2:
				return 1
			else:
				return 0


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return False
		left_rotate = str1[2:] + str1[:2]
		right_rotate = str1[-2:] + str1[:-2]
		if str2 == left_rotate or str2 == right_rotate:
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		k = str1[2:] + str1[:2]
		q = str1[::-1]
		w = q[2:] + q[:2]
		w = w[::-1]
		if k == str2:
			return 1
		elif w == str2:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return False
		s1 = str1[:2]
		s2 = str1[2:]
		s3 = s2 + s1
		s4 = str1[len(str1) - 2:] + str1[0:len(str1) - 2]
		if str2 in s3 or str2 in s4:
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		ans1 = str1[2:len(str1)]
		ans1 = ans1 + str1[0:2]
		ans2 = str1[-2:len(str1)]
		ans2 = ans2 + str1[0:-2]
		if ans1 == str2:
			return True
		elif ans2 == str2:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		a = str1[2:len(str1)]
		a = a + str1[0:2]
		b = str1[-2:len(str1)]
		b = b + str1[0:-2]
		if str2 == a:
			return True
		elif str2 == b:
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		d = 2
		l = len(str1)
		ls = str1[d:l] + str1[0:d]
		rs = str1[l - d:l] + str1[0:l - d]
		if ls == str2 or rs == str2:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		str3 = str1[2:] + str1[:2]
		str4 = str2[2:] + str2[:2]
		if str3 == str2 or str1 == str4:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, a, b):
		if a[2:] + a[:2] == b or a[-2:] + a[:-2] == b:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		s1 = str1[2:] + str1[0:2]
		s2 = str1[len(str1) - 2:] + str1[0:len(str1) - 2]
		if s1 == str2 or s2 == str2:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return 0
		sanyam = str1[2:] + str1[0:2]
		sanyam1 = str2[2:] + str2[0:2]
		if sanyam == str2 or sanyam1 == str1:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		if str1[:2] == str2[-2:] and str1[2:] == str2[:-2]:
			return 1
		elif str2[:2] == str1[-2:] and str2[2:] == str1[:-2]:
			return 1
		return 0


class Solution:

	def isRotated(self, s1, s2):
		if len(s1) != len(s2):
			return False
		left_rotated = s1[2:] + s1[:2]
		right_rotated = s1[-2:] + s1[:-2]
		return s2 in (left_rotated, right_rotated)


class Solution:

	def isRotated(self, str1, str2):
		sub1 = str1[2:] + str1[:2]
		sub2 = str1[-2:] + str1[:-2]
		if sub1 == str2 or sub2 == str2:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		clock = str1[2:] + str1[0:2]
		anti = str1[len(str1) - 2:] + str1[0:len(str1) - 2]
		if str2 == clock or str2 == anti:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		y = str1[:2]
		z = str1[2:]
		p = str1[len(str1) - 2:]
		q = str1[:len(str1) - 2]
		if p + q == str2 or q + p == str2:
			return True
		elif y + z == str2 or z + y == str2:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		if str1[2:] + str1[:2] == str2:
			return 1
		elif str1[-2:] + str1[:-2] == str2:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return False
		if len(str1) < 3:
			return str1 == str2
		left_rot = str1[2:] + str1[:2]
		right_rot = str1[-2:] + str1[:-2]
		return str2 == left_rot or str2 == right_rot


class Solution:

	def isRotated(self, str1, str2):
		str11 = str1[2:] + str1[:2]
		str12 = str1[-2:] + str1[:-2]
		if str11 == str2 or str12 == str2:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, s1, s2):
		if s1[2:] + s1[:2] == s2:
			return 1
		if s1[-2:] + s1[:-2] == s2:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		l1 = str1[0:2]
		l2 = str1[2:]
		r1 = str1[0:-2]
		r2 = str1[-2:]
		if l2 + l1 == str2 or r2 + r1 == str2:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		(cwise, acwise) = (True, True)
		n = len(str1)
		for i in range(n):
			if str1[i] != str2[(i + 2) % n]:
				cwise = False
		for i in range(n):
			if str1[(i + 2) % n] != str2[i]:
				acwise = False
		return cwise or acwise


class Solution:

	def isRotated(self, str1, str2):
		a = str1[2:len(str1)] + str1[0:2]
		b = str1[len(str1) - 2:len(str1)] + str1[0:len(str1) - 2]
		if a == str2 or b == str2:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		anticlock_string = str2[-2:] + str2[0:-2]
		clock_string = str2[2:] + str2[:2]
		if anticlock_string == str1 or clock_string == str1:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		aclock = str1[2:] + str1[0:2]
		clock = str1[-2:] + str1[0:-2]
		if aclock == str2 or clock == str2:
			return 1
		else:
			return 0


class Solution:

	def isRotated(self, str1, str2):
		k = list(str1)
		k.append(k.pop(0))
		k.append(k.pop(0))
		k = ''.join(k)
		n = list(str1)
		n.insert(0, n.pop(-1))
		n.insert(0, n.pop(-1))
		n = ''.join(n)
		if k == str2 or n == str2:
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		str1_1 = str1[2:] + str1[:2]
		if str1_1 == str2:
			return 1
		str2_1 = str2[2:] + str2[:2]
		if str1 == str2_1:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		anti_str1 = str1[-2:] + str1[:-2]
		clock_str1 = str1[2:] + str1[:2]
		return anti_str1 == str2 or clock_str1 == str2


class Solution:

	def isRotated(self, str1, str2):
		i = 0
		while i < len(str1):
			if str1[i] != str2[(i + 2) % len(str2)]:
				break
			i += 1
		if i == len(str2):
			return True
		i = 0
		while i < 2:
			if str1[i] != str2[i + len(str1) - 2]:
				break
			i += 1
		if i != 2:
			return False
		while i < len(str1):
			if str1[i] != str2[i - 2]:
				break
			i += 1
		if i == len(str2):
			return True
		return False


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return 0
		if len(str1) <= 2:
			return 0
		p = list(str1)
		a = p[0]
		b = p[1]
		for i in range(2, len(p)):
			p[i - 2] = p[i]
		p[-2] = a
		p[-1] = b
		k = ''.join(p)
		q = list(str1)
		c = q[-2]
		d = q[-1]
		for i in range(len(q) - 1, 1, -1):
			q[i] = q[i - 2]
		q[0] = c
		q[1] = d
		g = ''
		g = ''.join(q)
		if k == str2 or g == str2:
			return 1
		return 0


class Solution:

	def isRotated(self, str1, str2):
		if len(str1) != len(str2):
			return False
		if len(str1) < 2:
			return False
		x = len(str1)
		return str1[2:] + str1[:2] == str2 or str1[-2] + str1[-1] + str1[:x - 2] == str2


class Solution:

	def isRotated(self, str1, str2):
		n1 = len(str1)
		n2 = len(str2)
		if n1 != n2:
			return 0
		else:
			sc = str1[2:] + str1[:2]
			sac = str1[n1 - 2:] + str1[:n1 - 2]
			return 1 if sc == str2 or sac == str2 else 0


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		return len(str1) == len(str2) and (str1[2:n] + str1[0:2] == str2[:] or str1[n - 2:n] + str1[0:n - 2] == str2[:])


class Solution:

	def isRotated(self, str1, str2):
		n = len(str1)
		m = len(str2)
		if n != m:
			return False
		if n < 2:
			return str1 == str2
		a_str = str1[2:] + str1[0:2]
		aa_str = str1[n - 2:] + str1[0:n - 2]
		return a_str == str2 or aa_str == str2


class Solution:

	def isRotated(self, str1, str2):
		if str1 == str2:
			return 1
		elif len(str1) != len(str2):
			return 0
		n1 = len(str1)
		k = n1
		c1 = str1
		ac2 = str1
		temp1 = c1[:2]
		c1 = c1[2:] + temp1
		temp2 = ac2[n1 - 2:n1]
		ac2 = temp2 + ac2[:n1 - 2]
		if c1 == str2 or ac2 == str2:
			return 1
		return 0


class Solution:

	def isRotated(self, a, b):
		if len(a) != len(b):
			return False
		clockwise = a[-2:] + a[:-2]
		anticlockwise = a[2:] + a[:2]
		if clockwise == b or anticlockwise == b:
			return True
		else:
			return False


class Solution:

	def isRotated(self, str1, str2):
		N = len(str1)
		A = str1 + str1
		if str2 in A:
			if A.index(str2) == 2 or A.index(str2) == N - 2:
				return True
		return False