Ctrl K

Between Two Matching Letters

Easy
You get a string text. Pick two equal letters in it and count the characters strictly between them. Return the largest such count. If two equal letters are adjacent, there are 0 characters between them. If no letter repeats, return -1.
Example 1
text="pencil" → -1

All letters are different, so there is no matching pair and the answer is -1.

Example 2
text="banana" → 3

The first and the last 'a' sit at positions 1 and 5 with "nan" between them, 3 characters. Nothing is wider.

Example 3
text="zz" → 0
Constraints
  • 1 ≤ len(text) ≤ 300
  • 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 EasySame Letter Cipher EasySame Letters, Reshuffled EasySame When Flipped EasyShuffle Into a Palindrome Easy
loading Python…
Input
text="pencil"
Expected
-1

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