Substrings That Begin and End With the Same Letter
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 76 ms)β
| Metric | Value |
|---|---|
| Runtime | 76 ms |
| Memory | 40.7 MB |
| Date | 2021-12-29 |
Solution
public class Solution {
public long NumberOfSubstrings(string s) {
long n = s.Length;
long count = 0;
long[] occurences = new long[26];
for (int i = 0; i < n; i++)
{
occurences[s[i] - 'a'] += 1;
}
for (int i = 0; i < 26; i++)
{
count += occurences[i] > 0 ? (occurences[i] * (occurences[i] + 1)) / 2 : 0;
}
return count;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |