Maximum Repeating Substring
Real-world version: Longest Run of a Repeated TagThe 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.
def longest_tag_run(stream: str, tag: str) -> int:public int longestTagRun(String stream, String tag)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 16 test cases: 16 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's