i am getting TLE error on codechef problem Lapindromes link=>https://www.codechef.com/problems/LAPIN/

codechef

  1. #include<stdio.h>

  2. int main()

  3. {int a,i,j,k=0,l=0;

  4. char ch[1000],*p;

  5. scanf("%d",&a);

  6. while(a--)

  7. {scanf("%s",ch);

  8. p=ch;

  9. while(*p!='\0')

  10. {k++;

  11. p++;

  12. }

  13. for(i=0;i=k/2-1;i++)

  14. for(j=k-1;j>k/2-1;j--)

  15. {if(ch[i]==ch[j])

  16. l++;

  17. }

  18. if(l==k/2)

  19. printf("YES\n");

  20.  

  21. else printf("NO\n");

  22. }

  23. return 0;

  24. }

avinashexpc
Avinash Kumar
avinashexpc

Please Log in to answer

Note: Your answer should not be too short. Please wait a few seconds for the editor to load



Please make sure the answer is not too short

1 Answer

preda2or
preda2or 02:16, Apr 16
Shubham Kumar Gupta

your code's going into an infinite loop due to the condition in this loop which always remains true,

for(i=0;i=k/2-1;i++)

Your logic isn't correct either. I think you misunderstood the question. The question doesn't ask you to find if a given string is palindrome or not but whether two halves of the string have same characters and same frequency of each characters.

Hint : use hashing.

And from next time use the insert code snippet option to paste the code..makes it easier to read code. :)




Please make sure the answer is not too short
0 Upvotes
Comments
avinashexpc (Avinash Kumar) 09:14, Apr 16

yes,i got it,that's my mistake....