Getting TLE in Chef and String

My solution is running on linear time but even after i am getting TLE.

For loop of test cases you have given condition while(t–) but it should be while(t–>0).

If you are saying while loop is not terminating then you are wrong because i always use this while loop for testcase and it always works.

Use substr() @stream_cipher

But substr also runs in linear time see these posts link1,link2

I think it is O(1) of substring and I got ac after doing it.

your approach isn’t efficient.
it’s simple:
string with odd length has only one type of character
string with even length: for all i, a[i] must be equal to a[i+2].
:slightly_smiling_face:

https://stackoverflow.com/questions/15400508/string-concatenation-complexity-in-c-and-java
Read this and you will understand that the complexity of your code is not linear.

bro use append function to find l and r. The time complexity would be O(1) for every string.

naive approach -> define 2 strings , 1st one being left-rotation of the original , 2nd being the right-rotation , if they are equal , print yes

	for _ in range(int(input())):
		s=input()
		ans=0
		sl=s[1:]+s[0]
		sr=s[-1]+s[:-1]
		if sl==sr:
			print("YES")
		else:
			print("NO")

okay got it thanks :slight_smile: