Skip to content

Commit 6003d69

Browse files
committed
fix(is_similar): improve performance from O(n) to O(1)
1 parent adbbcc3 commit 6003d69

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

src/rotate_group_words/main.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,9 @@ def group_words(key: str, value: list[str]):
1515
pass
1616

1717

18-
def is_similar(source_word: str, other_word: str) -> bool:
19-
"""
20-
# get the length of the other_word
21-
# rotate all possible forms (right to left)
22-
#iterate through the lenght of the word and rotate_word()
23-
# compare each rotation with the source
24-
# if they match return True
25-
# else return False
26-
"""
27-
28-
is_similar = False
29-
for i in range(len(other_word)):
30-
new_form = rotate_word(other_word, i)
31-
is_similar = new_form == source_word
32-
if is_similar:
33-
break
34-
35-
return is_similar
18+
def is_similar(a: str, b: str) -> bool:
19+
"""Check if b is a rotation of a"""
20+
return len(a) == len(b) and b in (a + a)
3621

3722

3823
def rotate_word(word: str, count_of_rotation: int) -> str:

0 commit comments

Comments
 (0)