-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountPalindromicSubsequence.ts
32 lines (26 loc) · 1.04 KB
/
countPalindromicSubsequence.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
function countPalindromicSubsequence(s: string): number {
const seen: Set<string> = new Set();
const firstOccurrence: number[] = new Array(26).fill(-1);
const lastOccurrence: number[] = new Array(26).fill(-1);
for (let i = 0; i < s.length; i++) {
const charIdx: number = s.charCodeAt(i) - 97;
if (firstOccurrence[charIdx] === -1) {
firstOccurrence[charIdx] = i;
}
lastOccurrence[charIdx] = i;
}
for (let charIdx = 0; charIdx < 26; charIdx++) {
const start: number = firstOccurrence[charIdx];
const end: number = lastOccurrence[charIdx];
if (start !== -1 && end !== -1 && start < end) {
const middleChars: Set<string> = new Set();
for (let i = start + 1; i < end; i++) {
middleChars.add(s[i]);
}
for (const middle of middleChars) {
seen.add(String.fromCharCode(charIdx + 97) + middle + String.fromCharCode(charIdx + 97));
}
}
}
return seen.size;
};