→ DJYOMIYAHINA: 別捲了 01/04 12:14
※ 編輯: dont (194.195.89.211 日本), 01/04/2025 12:14:50
1930. Unique Length-3 Palindromic Subsequences
## 思路
先掃一次記錄每個字元 第一次跟最後一次出現的index
然後再看範圍內有幾種字元
## Code
```cpp
class Solution {
public:
int countPalindromicSubsequence(string s) {
vector<int> first(26, -1);
vector<int> last(26, -1);
int n = s.size();
for (int i=0; i<n; ++i) {
if (first[s[i]-'a'] == -1)
first[s[i]-'a'] = i;
last[s[i]-'a'] = i;
}
int res = 0;
for (int i=0; i<26; ++i) {
if (first[i] == -1)
continue;
bitset<26> seen = 0;
for (int j = first[i] + 1; j < last[i]; ++j)
seen[s[j]-'a'] = 1;
res += seen.count();
}
return res;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 194.195.89.211 (日本)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1735964067.A.F44.html