Ezvor

2955. Number of Same-End Substrings

MediumArray Preparing judge… LeetCode
ArrayHash TableStringCountingPrefix Sum

Hints

Hint 1
If there are t occurrences of a character in a substring, there exists t * (t + 1) / 2 Same-End substrings with that character.
Hint 2
Try to calculate the number of occurrences of a character in a substring in O(1) using partial sum.