Ctrl K

One-Letter Stretches

Easy
You are given a string text of lowercase English letters. Count its substrings made of a single repeated letter, such as "q" or "ppp". A substring is a contiguous piece of the string; equal substrings at different positions are counted separately.
Example 1
text="xxyxx" → 7

Each of the five letters is a one-letter substring, plus two pieces "xx". That makes 7.

Example 2
text="mmmm" → 10

4 substrings of length 1, 3 of length 2, 2 of length 3 and 1 of length 4: 4 + 3 + 2 + 1 = 10.

Example 3
text="abc" → 3

No two neighbors match, so only the three single letters count.

Constraints
  • 1 ≤ len(text) ≤ 1000
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasySum of Binary Strings EasySpreadsheet Column Label EasySpreadsheet Column Index EasyCounting Off by Threes and Fives Easy
loading Python…
Input
text="xxyxx"
Expected
7

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