3
int countPalindromicSubsequence(string s) {4
vector<pair<int, int>> v(26, {-1, -1}); // to store first occurance and5
// last occurance of every alphabet.7
int n = s.length(); // size of the string9
for (int i = 0; i < n; i++) {10
if (v[s[i] - 'a'].first == -1)11
v[s[i] - 'a'].first = i; // storing when alphabet appered first time.13
v[s[i] - 'a'].second = i; // else whenever it appears again. So that the14
// last occurrence will be stored at last.18
for (int i = 0; i < 26; i++) { // traversing over all alphabets.20
if (v[i].second != -1) { // only if alphabet occured second time.22
unordered_set<char> st; // using set to keep only unique elements between the range.24
for (int x = v[i].first + 1; x < v[i].second; x++)25
st.insert(s[x]); // set keeps only unique elemets.27
ans += ((int)st.size()); // adding number of unique elements to the answer.