1 var longestPalindrome = function(s) {
2 if (s.length == 0) {
3 return s;
4 }
5
6 var n = s.length;
7 var longest = s[0];
8
9 for (var i = 0; i < n; i++) {
10 var p1 = expandAroundCenter(s, i, i);
11 if (p1.length > longest.length) {
12 longest = p1;
13 }
14
15 var p2 = expandAroundCenter(s, i, i + 1);
16 if (p2.length > longest.length) {
17 longest = p2;
18 }
19 }
20
21 return longest;
22 };
23
24 var expandAroundCenter = function(s, c1, c2) {
25 var l = c1,
26 r = c2,
27 n = s.length;
28
29 while (l > -1 && r < n && s[l] == s[r]) {
30 l--;
31 r++;
32 }
33
34 return s.substr(l + 1, r - l - 1);
35 };