Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc5a73a

Browse files
committed
add: longestPalindromicSubstring.js
1 parent c643596 commit dc5a73a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎src/longestPalindromicSubstring.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
6+
var longestPalindrome = function(s) {
7+
if (!s) return '';
8+
var con = [],
9+
l = s.length,
10+
max = 1,
11+
start = 0;
12+
for (var i = 0; i < l; ++i) {
13+
con[i] = [];
14+
con[i][i] = 1;
15+
}
16+
17+
for (var k = 2; k <= l; ++k) {
18+
for (var i = 0, j = i + k - 1; i < l - 1 && j < l; ++i, ++j) {
19+
if ((con[i+1][j-1] || i + 1 === j) && s[i] === s[j]) {
20+
con[i][j] = 1;
21+
if (max < k) {
22+
max = k;
23+
start = i;
24+
}
25+
}
26+
}
27+
}
28+
return s.substr(start, max);
29+
};
30+
31+
var t1 = new Date();
32+
console.log(longestPalindrome("babad"));
33+
console.log(new Date() - t1);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /