I was solving this problem: Cyclic shift and I managed to solve the problem and write the code however with one flaw. My code seems to be exceeding the time limit of the test cases. This is my code:
def cycle(s, n):
return s[-n:] + s[:-n]
t = int(input())
for x in range(t):
n,k = map(int,input().split())
hello = input()
count = 0
for y in range(k):
hello2 = hello
for z in range(len(hello)):
shifted = cycle(hello2, 1)
hello2 = shifted
if shifted == hello:
shifted = cycle(hello2, 1)
hello = shifted
break
count += 1
print(count+1)
How can I write this in a way where it would not exceed the time limit of the test cases? I would also appreciate it if you can explain your code in your answer(even 1-2 sentences will be fine) so that I can understand and learn what you did and how the code is working. Thanks!
EDIT:

