All problems
0816EasyStringDynamic ProgrammingString Matching

Longest Run of a Repeated Tag

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1668Maximum Repeating Substring

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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