Skip to content

Latest commit

 

History

History
61 lines (44 loc) · 1.47 KB

_2083. Substrings That Begin and End With the Same Letter.md

File metadata and controls

61 lines (44 loc) · 1.47 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 10, 2024

Last updated : July 01, 2024


Related Topics : Hash Table, Math, String, Counting, Prefix Sum

Acceptance Rate : 74.1 %


Solutions

Python

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

C

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;
}