Problem Description
Given a 0-indexed string s consisting only of lowercase English letters, count the number of substrings (contiguous non-empty sequences) that begin and end with the same letter. Each individual character is considered a valid substring.
Key Insights
- Every single character is a valid substring (counts as 1).
- For any letter that appears f times, the number of substrings that start and end with that letter is f * (f + 1) / 2.
- Instead of checking all possible substrings, count the frequency of each letter and use the mathematical formula above.
- This approach leverages counting and simple arithmetic instead of nested loops.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the string, since we traverse the string once. Space Complexity: O(1), because we only need an array or dictionary of fixed size (26 for lowercase letters).
Solution
We solve the problem by:
- Iterating over the string to count the frequency of each letter.
- For each letter with frequency f, adding f * (f + 1) / 2 to the result.
- Returning the sum of these values. The key is to note that each letter at a given frequency contributes both its individual occurrences (as 1-length substrings) and all pairs of positions that form valid substrings.