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 hh
i and h
hi 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+1
unique insertion positions - For a character
c
(ins
) with frequencyf
, there aren+1-f
unique insertion positions
Derivation:
- New Characters: Each of the
26-k
characters (wherek
is the number of distinct characters ins
) can be inserted into any of then+1
positions, yielding(26-k)(n+1)
unique strings - Existing Characters: For each character
c
with frequencyf
:- Inserting
c
adjacent to existing instances ofc
may produce duplicate strings. For example, insertingc
before or after an existingc
merges into the same run, reducing unique positions byf
- Thus, the number of unique insertions for
c
isn+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;
}