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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 11. ⌘Enter runs, ⌘⇧Enter submits.