Show problems as
Classic problem #1668EasyStringDynamic ProgrammingString Matching

Maximum Repeating Substring

Real-world version: Longest Run of a Repeated Tag
Tracked in this browser only
Read Maximum Repeating Substring on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A device writes a single line of lowercase letters, given as stream, and a watchdog looks for a marker, given as tag.

The marker is said to run k times when k copies of the whole marker, written one straight after another with nothing in between, appear as a contiguous stretch of stream.

Return the largest k for which that happens. If the marker never appears in the line at all, return 0.

Examples

Example 1

Input
stream = "xxabcabcabcyy", tag = "abc"
Output
3

Three copies of the marker written together spell "abcabcabc", and that stretch sits in the line starting at position 2.

Example 2

Input
stream = "abxxab", tag = "ab"
Output
1

The marker appears at position 0 and again at position 4, but "abab" is nowhere in the line, so the longest run is a single copy.

Example 3

Input
stream = "hi", tag = "hihi"
Output
0

The marker is longer than the line, so not even one copy fits.

Constraints

  • 1 <= stream.length <= 100
  • 1 <= tag.length <= 100
  • stream and tag hold lowercase English letters only.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def longest_tag_run(stream: str, tag: str) -> int:
Java
public int longestTagRun(String stream, String tag)