Skip to content

Commit 1f592b3

Browse files
authored
add 5-Longest-Palindromic-Substring
1 parent c23d249 commit 1f592b3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function longestPalindrome(s) {
2+
let res = "";
3+
let resLen = 0;
4+
5+
for (let i = 0; i < s.length; i++) {
6+
let l = i;
7+
let r = i;
8+
9+
while (l >= 0 && r < s.length && s[l] === s[r]) {
10+
if (r - l + 1 > resLen) {
11+
res = s.slice(l, r + 1);
12+
resLen = r - l + 1;
13+
}
14+
15+
l -= 1;
16+
r += 1;
17+
}
18+
19+
l = i;
20+
r = i + 1;
21+
22+
while (l >= 0 && r < s.length && s[l] === s[r]) {
23+
if (r - l + 1 > resLen) {
24+
res = s.slice(l, r + 1);
25+
resLen = r - l + 1;
26+
}
27+
28+
l -= 1;
29+
r += 1;
30+
}
31+
}
32+
33+
return res;
34+
}

0 commit comments

Comments
 (0)