Largest Substring Between Two Equal Characters

EasyStringshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a string s, pick two positions i < j holding the same character and look at the substring strictly between them (it may be empty). Return the greatest possible length of such a substring. If no character appears twice, return -1.

Examples

Input: s = "aa"
Output: 0
The two a's are adjacent, so the substring between them is empty.
Input: s = "abca"
Output: 2
"bc" lies between the two a's.
Input: s = "cbzxy"
Output: -1
No character repeats.

Constraints

  • 1 <= s.length <= 300
  • `s` contains only lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems