Ctrl K

Same Letter at Both Ends

Medium
You are given a string text and a letter letter. Count the non-empty substrings of text that both start and end with letter. Substrings at different positions count separately, even if they read the same. A one-character substring equal to letter counts too.
Example 1
text='banana', letter='a' → 6

There are three a's. The three single "a"s qualify, plus "ana", "anana" and a second "ana": 6 in total.

Example 2
text='kiwi', letter='p' → 0

There is no "p" in the string, so there are no such substrings.

Example 3
text='ooo', letter='o' → 6
Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters
  • letter is a single lowercase English letter
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPieces With Matching Ends MediumThe Growing String MediumHow Far a Tweaked Route Can Take You MediumLength of a Growing Word HardNumber in Roman Numerals Medium
loading Python…
Input
text='banana', letter='a'
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.