Search⌘ K
AI Features

Solution: Find Longest Self-Contained Substring

Explore how to identify the longest self-contained substring in a lowercase string by using hash maps to track character first and last occurrences. Understand how to expand and validate substring windows efficiently to solve this problem in linear time.

Statement

You are given a string, s, consisting of lowercase English letters. Your task is to find the length of the longest self-contained substring of s.

A substring t of s is called self-contained if:

  • t is not equal to the entire string s.

  • Every character in t does not appear anywhere else in s (outside of t).

In other words, all characters in t are completely unique to that substring within the string s.

Return the length of the longest self-contained substring. If no such substring exists, return -1.

Constraints:

  • 22 \leq ...