Skip to content

Commit

Permalink
Create 828.Count-Unique-Characters-of-All-Substrings-of-a-Given-Strin…
Browse files Browse the repository at this point in the history
…g_v2.cpp
  • Loading branch information
wisdompeak authored May 2, 2022
1 parent 40f93bd commit 6dfc280
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int uniqueLetterString(string s)
{
int n = s.size();
vector<vector<int>>pos(26);

for (int k=0; k<26; k++)
pos[k].push_back(-1);
for (int i=0; i<n; i++)
pos[s[i]-'A'].push_back(i);
for (int i=0; i<n; i++)
pos[s[i]-'A'].push_back(n);

int ret = 0;
for (int k=0; k<26; k++)
for (int i=1; i<pos[k].size()-1; i++)
{
ret += (pos[k][i]-pos[k][i-1])*(pos[k][i+1]-pos[k][i]);
}

return ret;
}
};

0 comments on commit 6dfc280

Please sign in to comment.