Skip to content

Commit a7417c3

Browse files
authored
Update 5-longest-palindromic-substring.js
1 parent a64f4fc commit a7417c3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

5-longest-palindromic-substring.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const longestPalindrome = function(s) {
6+
let res = ''
7+
for(let i = 0, len = s.length; i < len; i++) {
8+
let s1 = chk(s,i,i), s2 = chk(s,i,i+1)
9+
if(s1.length > res.length) res = s1
10+
if(s2.length > res.length) res = s2
11+
}
12+
return res
13+
};
14+
15+
function chk(s, i, j) {
16+
for(; i>= 0 && j < s.length; i--, j++) {
17+
if(s[i] !== s[j]) break
18+
}
19+
return s.slice(i+1, j)
20+
}
21+
22+
// another
23+
124
/**
225
* @param {string} s
326
* @return {string}

0 commit comments

Comments
 (0)