-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsolution.cpp
36 lines (32 loc) · 883 Bytes
/
solution.cpp
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
class Solution
{
public:
int countPalindromicSubsequence(string s)
{
vector<int> first(26, -1), last(26, -1);
int n = s.size();
// Record first and last occurrences
for (int i = 0; i < n; i++)
{
int index = s[i] - 'a';
if (first[index] == -1)
first[index] = i;
last[index] = i;
}
int result = 0;
// Count unique middle characters for each letter
for (int i = 0; i < 26; i++)
{
if (first[i] != -1 && last[i] > first[i])
{
unordered_set<char> middleChars;
for (int j = first[i] + 1; j < last[i]; j++)
{
middleChars.insert(s[j]);
}
result += middleChars.size();
}
}
return result;
}
};