1
var countPalindromicSubsequence = function (s) {
2
const charToIndices = {};
3
for (let i = 0; i < s.length; i++) {
4
const char = s[i];
5
if (charToIndices[char]) {
6
charToIndices[char].push(i);
7
} else {
8
charToIndices[char] = [i];
9
}
10
}
11

12
let count = 0;
13
for (const currChar in charToIndices) {
14
if (charToIndices[currChar].length < 2) continue;
15
const currCharIndices = charToIndices[currChar];
16
const firstIndex = currCharIndices[0];
17
const lastIndex = currCharIndices[currCharIndices.length - 1];
18

19
for (const char in charToIndices) {
20
const indices = charToIndices[char];
21
let lo = 0;
22
let hi = indices.length;
23
while (lo < hi) {
24
const mid = (lo + hi) >> 1;
25
if (indices[mid] <= firstIndex) {
26
lo = mid + 1;
27
} else {
28
hi = mid;
29
}
30
}
31
if (indices[lo] && indices[lo] < lastIndex) {
32
count++;
33
}
34
}
35
}
36
return count;
37
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0