Skip to content
Onsite

Count Substrings Starting and Ending With Same Character

MediumP3Coding

Count the substrings of s that start and end with the same character. Substrings of length 1 count. Substrings are counted by position.

Example 1

Input
s = "abcab"
Output
7

Example 2

Input
s = "aaa"
Output
6

Example 3

Input
s = "a"
Output
1

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase letters.

Hints

Run checks the 3 visible tests, Submit all 11.