Create 828.cpp

master
Kirigaya Kazuto 2018-07-10 11:09:43 +08:00 committed by GitHub
parent a1f917027f
commit 12c169923e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 0 deletions

35
LeetCode-CN/828.cpp Normal file
View 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;
}
};