mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 828.cpp
This commit is contained in:
parent
a1f917027f
commit
12c169923e
35
LeetCode-CN/828.cpp
Normal file
35
LeetCode-CN/828.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
const static long long MOD = 1000000007;
|
||||||
|
int uniqueLetterString(string S) {
|
||||||
|
map<char, vector<int>> mp;
|
||||||
|
long long cnt = 0;
|
||||||
|
int slen = S.size();
|
||||||
|
for (int i = 0; i < slen; i++)
|
||||||
|
{
|
||||||
|
auto iter = mp.find(S[i]);
|
||||||
|
if (iter == mp.end())
|
||||||
|
{
|
||||||
|
tie(iter, ignore) = mp.insert(make_pair(S[i], vector<int>{-1}));
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int sz = iter->second.size();
|
||||||
|
int k = iter->second[sz - 2];
|
||||||
|
int j = iter->second[sz - 1];
|
||||||
|
cnt = (cnt + (j - k)*(i - j)) % MOD;
|
||||||
|
}
|
||||||
|
iter->second.push_back(i);
|
||||||
|
}
|
||||||
|
for (const auto& pr : mp)
|
||||||
|
{
|
||||||
|
int sz = pr.second.size();
|
||||||
|
int k = pr.second[sz - 2];
|
||||||
|
int j = pr.second[sz - 1];
|
||||||
|
cnt = (cnt + (j - k)*(slen - j)) % MOD;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user