← all problems

countBookendSubstrings 🌶️🌶️🌶️

A substring is a bookend when its first and last characters are the same letter. A single letter counts (it is both its own first and last character). Substrings are counted by where they sit, so the two a letters in "aba" each give their own one-letter bookend.

Write a method countBookendSubstrings that takes a String s and returns the number of bookend substrings in it.

For example, in "aba" the bookends are "a", "b", "a" and "aba", so countBookendSubstrings("aba") is 4. And countBookendSubstrings("abc") is 3.