Skip to content

Commit 395146f

Browse files
committed
Time: 133 ms (90.17%), Space: 22.7 MB (30.47%) - LeetHub
1 parent 3f8d882 commit 395146f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int countPalindromicSubsequence(string s) {
4+
int n = s.size();
5+
vector<vector<int>> ma(26);
6+
for (int i = 0 ; i < n ; i++){
7+
ma[s[i] - 'a'].push_back(i);
8+
}
9+
int ans = 0;
10+
for (int i = 0 ; i < 26 ; i++){
11+
if (ma[i].size() > 0){
12+
for (int j = 0 ; j < 26 ; j++){
13+
int idx = upper_bound(ma[j].begin(), ma[j].end(), ma[i][0]) - ma[j].begin();
14+
if (idx != ma[j].size() && ma[j][idx] < ma[i].back()){
15+
ans++;
16+
}
17+
}
18+
}
19+
}
20+
return ans;
21+
22+
}
23+
};

0 commit comments

Comments
 (0)