-
Notifications
You must be signed in to change notification settings - Fork 0
/
347.py
43 lines (30 loc) · 1.12 KB
/
347.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
"""
Problem:
You are given a string of length N and a parameter k. The string can be manipulated by
taking one of the first k letters and moving it to the end.
Write a program to determine the lexicographically smallest string that can be created
after an unlimited number of moves.
For example, suppose we are given the string daily and k = 1. The best we can create in
this case is ailyd.
"""
def generate_next(string: str, k: int) -> str:
return string[k:] + string[:k]
def get_lexicographically_smallest_string(string: str, k: int) -> str:
seen = set()
result = string
curr = generate_next(string, k)
while curr not in seen:
result = min(result, curr)
seen.add(curr)
curr = generate_next(curr, k)
return result
if __name__ == "__main__":
print(get_lexicographically_smallest_string("daily", 1))
print(get_lexicographically_smallest_string("salloo", 2))
# unlimited number of moves allowed (so the word of length 5 and k = 2 goes round)
print(get_lexicographically_smallest_string("daily", 2))
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(n ^ 2)
"""