forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_rotated.py
More file actions
31 lines (27 loc) · 711 Bytes
/
is_rotated.py
File metadata and controls
31 lines (27 loc) · 711 Bytes
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
"""
Given two strings s1 and s2, determine if s2 is a rotated version of s1.
For example,
is_rotated("hello", "llohe") returns True
is_rotated("hello", "helol") returns False
accepts two strings
returns bool
Reference: https://leetcode.com/problems/rotate-string/description/
"""
def is_rotated(s1, s2):
if len(s1) == len(s2):
return s2 in s1 + s1
else:
return False
"""
Another solution: brutal force
Complexity: O(N^2)
"""
def is_rotated_v1(s1, s2):
if len(s1) != len(s2):
return False
if len(s1) == 0:
return True
for c in range(len(s1)):
if all(s1[(c + i) % len(s1)] == s2[i] for i in range(len(s1))):
return True
return False