Skip to content

Commit 0a15d6a

Browse files
authored
Update 828.Count-Unique-Characters-of-All-Substrings-of-a-Given-String.cpp
1 parent d98f5fc commit 0a15d6a

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
class Solution {
2-
long long M = pow(10,9)+7;
32
public:
4-
int uniqueLetterString(string S)
5-
{
6-
vector<vector<int>>pos(26);
7-
int result=0;
8-
for (int i=0; i<S.size(); i++)
3+
int uniqueLetterString(string s)
4+
{
5+
int n = s.size();
6+
vector<vector<int>>pos(26); // pos[k]: the pos of letter k so far (by i-th)
7+
8+
int ret = 0;
9+
for (int i=0; i<n; i++)
910
{
10-
pos[S[i]-'A'].push_back(i);
11-
for (int i=0; i<26; i++)
11+
pos[s[i]-'A'].push_back(i);
12+
for (int k=0; k<26; k++)
1213
{
13-
if (pos[i].size()==1)
14-
result+=pos[i][0]+1;
15-
else if (pos[i].size()>1)
14+
if (pos[k].size()>=2)
1615
{
17-
int k = pos[i].size();
18-
result+=pos[i][k-1]-pos[i][k-2];
16+
int m = pos[k].size();
17+
ret += pos[k][m-1] - pos[k][m-2];
18+
}
19+
else if (pos[k].size()==1)
20+
{
21+
ret += pos[k][0]+1;
1922
}
20-
result = result%M;
2123
}
2224
}
23-
return result;
25+
return ret;
2426
}
25-
2627
};

0 commit comments

Comments
 (0)