1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int countPalindromicSubsequence(string s) { int n = s.size(), l[26] = {[0 ... 25] = n}, r[26] = {}, mask, ans = 0; for (int i = 0; i < n; ++i) { int idx = s[i] - 'a'; l[idx] = min(l[idx], i); r[idx] = i; } for (int i = 0; i < 26; ++i) { mask = 0; for (int j = l[i] + 1; j < r[i]; ++j) { int idx = s[j] - 'a'; mask |= (1 << idx); } ans += __builtin_popcount(mask); } return ans; } };
|