-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05.isRotation.py
43 lines (33 loc) · 938 Bytes
/
05.isRotation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Source: https://www.udemy.com/course/11-essential-coding-interview-questions/
# Problem: "Check if one array is a rotation of another"
# Example:
# [1,2,3,4,5,6,7],[4,5,6,7,1,2,3] -> True
# Questions:
# 1. Are there duplicates? No
# Brute-force solution:
# Generate all rotations and compare
# Approach:
# Fix the first one, find diff.
# Compare each A[i] with A[diff+i]. If diff + i >= len(A)
# use diff + i - len(A) instead
# Complexity:
# O(N) time
# O(1) space
def isRotation(arrayA, arrayB):
arraySize = len(arrayA)
if not arraySize == len(arrayB):
return False
for diff in range(arraySize):
if arrayB[diff] == arrayA[0]:
break
if arrayA[0] != arrayB[diff]:
return False
for i in range(arraySize):
indB = i + diff
if indB >= arraySize:
indB -= arraySize
if arrayA[i] != arrayB[indB]:
return False
return True
if __name__ == "__main__":
print(isRotation([1,2,3,4,5,6,7],[4,5,6,7,1,2,3]))