© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
3 replies
morry329#

✅ why does the test case fail?

This is from a longest palindrome puzzle on LeetCode https://leetcode.com/problems/longest-palindrome/solutions/
I am learning from a bunch of solutions, but I could not get why this solution fails the test case (as per screenshot below).
Here's the code I am experimenting. Could anyone kindly give me some clue?

`public class Solution {
    public int LongestPalindrome(string s) {
        int n = s.Length;
        int maxLength = 1, start = 0;
        for(int i = 0; i < s.Length; i++){
            for(int j = i; j < s.Length; j++){
                int flag = 1;

                for(int k = 0; k < (j-i+1)/2; k++){
                    if(s[i+k] != s[j-k]){
                        flag = 0;
                    }
                }

                if(flag != 0 && (j-i+1) > maxLength){
                    start = i;
                    maxLength = j - i+1;
                }
            }
        }
        return maxLength;

    }
}
`public class Solution {
    public int LongestPalindrome(string s) {
        int n = s.Length;
        int maxLength = 1, start = 0;
        for(int i = 0; i < s.Length; i++){
            for(int j = i; j < s.Length; j++){
                int flag = 1;

                for(int k = 0; k < (j-i+1)/2; k++){
                    if(s[i+k] != s[j-k]){
                        flag = 0;
                    }
                }

                if(flag != 0 && (j-i+1) > maxLength){
                    start = i;
                    maxLength = j - i+1;
                }
            }
        }
        return maxLength;

    }
}
Bildschirmfoto_2023-02-02_um_11.14.20.png
LeetCode
Longest Palindrome - LeetCode
Longest Palindrome - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
Longest Palindrome - LeetCode
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Test file not found case
C#CC# / help
4y ago
Not able to pass the test case correctly
C#CC# / help
2y ago
❔ ✅ My code is pretty close to the right solution, but one test case still fails
C#CC# / help
3y ago
Why does this work the way it does
C#CC# / help
8mo ago