Ctrl K

Longest Clean Stretch

Hard
A chat moderator checks the message text against a list of banned letter sequences banned. A stretch of the message (a run of consecutive characters) is **clean** if no sequence from banned appears entirely inside it. Return the length of the longest clean stretch. The empty stretch counts as clean, so the answer can be 0.
Example 1
text="moonbeam", banned=["oo", "ea"] → 4

The stretch onbe is clean: extending it left would add oo, extending it right would add ea. Nothing longer avoids both, so the answer is 4.

Example 2
text="aaaa", banned=["a"] → 0

The letter a itself is banned, so only the empty piece is clean.

Example 3
text="river", banned=["xyz"] → 5

The banned word never appears, so the whole text works.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • 1 ≤ len(banned) ≤ 10⁵
  • 1 ≤ len(banned[i]) ≤ 10
  • text and every banned[i] consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWord Blocks in Any Order HardShortest Covering Window HardEvery Way to Space the Words HardGlued Into a Palindrome HardMoving Window Median Hard
loading Python…
Input
text="moonbeam", banned=["oo", "ea"]
Expected
4

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