# Simple C++ solution (8ms, 13 lines)

• ``````string longestPalindrome(string s) {
if (s.empty()) return "";
if (s.size() == 1) return s;
int min_start = 0, max_len = 1;
for (int i = 0; i < s.size();) {
if (s.size() - i <= max_len / 2) break;
int j = i, k = i;
while (k < s.size()-1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
i = k+1;
while (k < s.size()-1 && j > 0 && s[k + 1] == s[j - 1]) { ++k; --j; } // Expand.
int new_len = k - j + 1;
if (new_len > max_len) { min_start = j; max_len = new_len; }
}
return s.substr(min_start, max_len);
}``````

• this is the most wonderful way I had ever seen

• Can you tell me how you calaulate the time is 8ms?Thank you.

• When the solution is accepted, click the "More Details" button. You can see the runtime and its distribution.

• Thank you。。。

• This post is deleted!

• This post is deleted!

• Could you please tell me how can we get the explicit Time Complexity of this method(algo) ..? Thank you .

• wonderful solution! Thanks~

• I think it has the best time complexity is O(N), which the inputs look like "aaaaaaa"; and the worst time complexity is O(N*N), which "the case scenarios are the inputs with multiple palindromes overlapping each other", such as "ababababab".

• Wonderful solution! Thanks~

I rewrite it with C and use pointers.
runtime: 0 ms

``````char* longestPalindrome(char* s) {
char *go,*max_start,*left,*right,*re;
int max_length,temp_length;
char *s_end;
s_end = s+strlen(s);
if (strlen(s) == 0) return s;
if (strlen(s) == 1) return s;
for(go = s,max_length = 1;go<s_end;)
{
if (s_end - go <= max_length * sizeof(char) / 2) break;
left = go,right = go;
while(*right==*(right+1)) right++;
go = right + 1;
while(left<s_end && left>s && *(left-1) == *(right+1))
{
right++;
left--;
}
temp_length = (right-left)/sizeof(char) + 1;
if(temp_length > max_length) {
max_start = left;
max_length = temp_length;
}
}
re =(char *) malloc(max_length*sizeof(char)+1);
go = re;
while(max_length)
{
*(go) = *(max_start);
go++;
max_start++;
max_length--;
}
*go = '\0';
return re;
``````

}

• This post is deleted!

• This kind of "13 lines" is actually due to poor writing style. :(

• This is still an O(N^2) algorithm right?

• Yes. It just avoids the discussion of odd and even cases.

• Brilliant!

I rewrite it using JAVA, runtime: 340ms

``````public String longestPalindrome(String s) {
if (s.isEmpty()) return "";
if (s.length() == 1) return s;
int min_start = 0, max_len = 1;
for (int i = 0; i < s.length(); ) {
if (s.length() - i <= max_len / 2) break;
int j = i, k = i;
while (k < s.length() - 1 && s.charAt(k + 1) == s.charAt(k)) ++k; // Skip duplicate characters.
i = k + 1;
while (k < s.length() - 1 && j > 0 && s.charAt(k + 1) == s.charAt(j - 1)) {
++k;
--j;
} // Expand.
int new_len = k - j + 1;
if (new_len > max_len) {
min_start = j;
max_len = new_len;
}
}
return s.substring(min_start, min_start + max_len);
}``````

• Actually the "if (s.size() == 1) return s;" line is not necessary.

• Good! no need to deal with even/odd problem.

• What's the complexity of it?

• Can someone explain the algorithm? I am not able to get my head around what i, j, k are trying to keep a track of?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.