Ctrl K

Longest Back-to-Back Run

Easy
You are given strings text and piece. Write piece several times in a row: with k = 3, "ab" becomes "ababab". Find the largest k for which k glued copies of piece appear in text as a contiguous substring. If piece does not appear in text at all, the answer is 0.
Example 1
text='xyxyxyz', piece='xy' → 3

"xyxyxy" is three copies of "xy" in a row, and it appears in the text. Four copies in a row do not.

Example 2
text='abcab', piece='ca' → 1

"ca" appears once, but "caca" does not.

Example 3
text='hello', piece='world' → 0
Constraints
  • 1 ≤ len(text) ≤ 100
  • 1 ≤ len(piece) ≤ 100
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStrings in a Range Without a Banned Word HardPeel Off a Repeated Start HardTail to the Front HardTarget From Word Beginnings MediumPhrase From Word Beginnings, Long Edition Hard
loading Python…
Input
text='xyxyxyz', piece='xy'
Expected
3

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