-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest-palindromic-substring.js
57 lines (45 loc) · 1.09 KB
/
longest-palindromic-substring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
5. Longest Palindromic Substring
Given a string s, return the longest palindromic substring in s.
A string is called a palindrome string if the reverse of that string is the same as the original string.
Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
*/
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (s) {
if (s.length <= 1) {
return s;
}
let start = 0;
let end = 0;
for (let i = 0; i < s.length; i++) {
const length1 = expandAroundCenter(s, i, i);
const length2 = expandAroundCenter(s, i, i + 1);
const maxLength = Math.max(length1, length2);
if (maxLength > end - start) {
start = i - maxLength / 2 + 1;
end = i + maxLength / 2;
}
}
return s.slice(start, end + 1);
};
const expandAroundCenter = (s, left, right) => {
let L = left;
let R = right;
while (L >= 0 && R < s.length && s[L] === s[R]) {
L--;
R++;
}
return R - L - 1;
};