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