Ctrl K

Longest Piece Seen Twice

Medium
You are given a string text. Find the longest piece of it (consecutive characters) that occurs in the string at least twice, and return its length. The occurrences may overlap. If no piece occurs twice, return 0.
Example 1
text="bananas" → 3

ana appears twice, starting at positions 1 and 3; occurrences may overlap. No piece of length 4 appears twice.

Example 2
text="qwerty" → 0

All letters are different, so nothing repeats.

Example 3
text="zzzz" → 3

zzz appears starting at position 0 and at position 1.

Constraints
  • 1 ≤ len(text) ≤ 2000
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTarget From Word Beginnings MediumLongest Repeated Fragment HardSuffixes Echoing the Start HardPhrase From Word Beginnings, Long Edition HardLongest Shared Run Medium
loading Python…
Input
text="bananas"
Expected
3

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