-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
41 lines (38 loc) · 1.12 KB
/
main.cpp
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
class Solution {
public:
int uniqueLetterString(string s) {
int n = s.size();
int count = 0;
for (char c = 'A'; c <= 'Z'; ++c) {
vector<int> indices = {-1};
for (int i = 0; i < n; ++i) {
if (s[i] == c) indices.push_back(i);
}
indices.push_back(n);
for (int i = 1; i < indices.size() - 1; ++i) {
count += (indices[i] - indices[i - 1]) *
(indices[i + 1] - indices[i]);
}
}
return count;
}
};
class Solution {
public:
int uniqueLetterString(string s) {
int n = s.size();
int indices[26][2];
memset(indices, -1, sizeof(indices));
int count = 0;
for (int i = 0; i < n; ++i) {
char c = s[i] - 'A';
count += (indices[c][1] - indices[c][0]) * (i - indices[c][1]);
indices[c][0] = indices[c][1];
indices[c][1] = i;
}
for (int c = 0; c < 26; ++c) {
count += (indices[c][1] - indices[c][0]) * (n - indices[c][1]);
}
return count;
}
};