-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path1930
57 lines (47 loc) · 1.71 KB
/
1930
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
int countPalindromicSubsequence(string s) {
// vector<vector<int>> v(26);
// for(int i=0;i<s.size();i++){
// v[s[i]-'a'].push_back(i);
// }
// int c=0;
// for(vector<int> i:v){
// vector<int> d=i;
// int t=d[0];
// int m=d[d.size()-1];
// for(auto j:v){
// if(j!=i){
// vector<int> p=j;
// for(int i=0;i<p.size();i++){
// if(p[i]>t && p[i]<m){
// c++;
// break;
// }
// }
// }
// }
// }
// return c;
vector<vector<int>> positions(26);
// Storing positions of each character in the string
for (int i = 0; i < s.size(); ++i) {
positions[s[i] - 'a'].push_back(i);
}
int count = 0;
// Iterating over characters
for (int i = 0; i < 26; ++i) {
vector<int> char_positions = positions[i];
// Checking if the character occurs at least twice
if (char_positions.size() > 1) {
int start = char_positions.front();
int end = char_positions.back();
// Checking distinct characters between start and end positions
set<char> distinct_chars(s.begin() + start + 1, s.begin() + end);
// Incrementing count by the number of distinct characters
count += distinct_chars.size();
}
}
return count;
}
};