Topics
Idea
If we have string s with n characters, then we got n+1 slots where a letter can be placed. For duplicates, the number of slots decrease by the freq of duplicates (this is an observation), e.g. “hi” has 3 slots where we can place letters other than ‘h’ and ‘i’. For letters ‘h’ and ‘i’, we only have 2 slots since hhi and hhi count as the same.
Note that we can come up with a closed form solution with this observation by analyzing two cases:
- Inserting a new character (not in
s) and inserting an existing character (already ins) - For a character
c(not ins), there aren+1unique insertion positions - For a character
c(ins) with frequencyf, there aren+1-funique insertion positions
Derivation:
- New Characters: Each of the
26-kcharacters (wherekis the number of distinct characters ins) can be inserted into any of then+1positions, yielding(26-k)(n+1)unique strings - Existing Characters: For each character
cwith frequencyf:- Inserting
cadjacent to existing instances ofcmay produce duplicate strings. For example, insertingcbefore or after an existingcmerges into the same run, reducing unique positions byf - Thus, the number of unique insertions for
cisn+1-f. Summing over all existing characters gives `sum_over_k(n+1-freq[k])
- Inserting
Thus, we have a formula:
Code
void solve() {
string s;
cin >> s;
vector<int> cnt(26, 0);
for (char &c : s) {
cnt[c - 'a']++;
}
int slots = s.length() + 1;
ll res = 0;
for (int &v : cnt) {
res += (slots - v);
}
cout << res << endl;
}