Ctrl K

Longest Sealed Block

Hard
You get a string text of lowercase English letters. Call a non-empty block of consecutive characters **sealed** if it is not the whole string and every letter that appears in it appears nowhere else in text outside the block. Return the length of the longest sealed block, or -1 if there is none.
Example 1
text="xyyxzz" → 4

The block "xyyx" holds every x and y of the string and no z. The whole string does not count, and "xyyxz" fails because one z is left outside.

Example 2
text="mnmn" → -1

Any block shorter than the whole string takes an m or an n but not all of its copies.

Example 3
text="pqrp" → 2

The blocks "q", "r" and "qr" all work, and "qr" is the longest. A block with a p has to take both p's, which means the whole string.

Constraints
  • 2 ≤ len(text) ≤ 5·10⁴
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFlowers in Bloom on Visit Day HardBalanced Stretches of a String HardShuffle Two Windows to a Mirror HardFew Zeros or Few Ones: Range Queries HardPump Rods at Their Peak Hard
loading Python…
Input
text="xyyxzz"
Expected
4

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