< Back to forum

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

  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. }

Asked by: Avinash_Kumar on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

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. :)

Shubham_Kumar_Gupta last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.