Similar question has been asked in JAVA, but can someone help me in improving the code: And explain what would be the time complexity and space complexity of my code. My code checks if two arrays are rotated version of each other:
list1 = [1, 2, 3, 4, 5, 6, 7]
list2b = [4, 5, 6, 7, 1, 2, 3]
is_rotation(list1, list2b) should return True.
list2c = [4, 5, 6, 9, 1, 2, 3]
is_rotation(list1, list2c) should return False.
My code:
def is_rotation (list1,list2):
i = 0
j = 0
k = 0
result = True
if len(list1) != len(list2):
return False
while i < len(list1) -1 and j < len(list1) -1:
if list1[i] == list2[j]:
i = i +1
j = j +1
break
elif list1[i] > list2[j]:
j = j +1
else:
i = i +1
else:
return False
for l in range(i,len(list1)):
if i == j:
if list1[i] != list2[j]:
return False
elif list1[i] != list2[j] or list2[i] != list1[k]:
return False
else:
i = i +1
j = j +1
k = k +1
return result