1
class Solution {
2
public:
3
long long appealSum(string s) {
4
int n = s.size();
5
long long ans = 0;
6
for (char ch = 'a'; ch <= 'z'; ch++) // we are finding the number of substrings containing at
7
// least 1 occurence of ch
8
{
9
int prev = 0; // prev will store the previous index of the charcter ch
10
for (int i = 0; i < n; i++) {
11
if (s[i] == ch)
12
prev = i + 1; // if the current character is equal to ch , then the
13
// no. of substring ending at i and having at least one
14
// occurence of ch will be i+1 .
15

16
ans += prev; // else the no. of substrings ending at i and having at
17
// least one occurence of ch will be the equal to, the
18
// previous index of ch.
19
}
20
}
21
return ans; // TC - O(n*26) , SC - O(1)
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0