-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlongestPalindrome.ts
48 lines (39 loc) · 1.23 KB
/
longestPalindrome.ts
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
type LongestPalindrome = (s: string) => string;
/**
* Accepted
*/
export const longestPalindrome: LongestPalindrome = (s) => {
const n = s.length;
// If the string is empty or has only one character, it's itself a palindrome
if (n < 2) return s;
let start = 0; // Starting index of the longest palindromic substring
let maxLength = 1; // Length of the longest palindromic substring
// Initialize a 2D DP array
const dp: boolean[][] = Array.from({ length: n }, () => Array(n).fill(false));
// All substrings of length 1 are palindromes
for (let i = 0; i < n; i++) {
dp[i][i] = true;
}
// Check for substrings of length 2
for (let i = 0; i < n - 1; i++) {
if (s[i] === s[i + 1]) {
dp[i][i + 1] = true;
start = i;
maxLength = 2;
}
}
// Check for substrings of length greater than 2
for (let len = 3; len <= n; len++) {
for (let i = 0; i <= n - len; i++) {
// Ending index of substring
const j = i + len - 1;
if (s[i] === s[j] && dp[i + 1][j - 1]) {
dp[i][j] = true;
start = i;
maxLength = len;
}
}
}
// Extract the longest palindromic substring from start index and maxLength
return s.substring(start, start + maxLength);
};