All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 10, 2024
Last updated : July 01, 2024
Related Topics : Hash Table, Math, String, Counting, Prefix Sum
Acceptance Rate : 74.1 %
class Solution:
def numberOfSubstrings(self, s: str) -> int:
counter = Counter(s)
output = len(s)
for key in counter :
if counter.get(key, 0) > 1 :
output += (counter.get(key, 0)) * (counter.get(key, 0) - 1) // 2
return output
long long numberOfSubstrings(char* s) {
long numChars[26] = {0};
char* temp = s;
while (*temp) {
numChars[*temp - 'a']++;
temp++;
}
long long output = 0;
for (int i = 0; i < 26; i++) {
output += (numChars[i] * (numChars[i] + 1)) / 2;
}
return output;
}