All problems
0089EasyTwo PointersStringString MatchingZ AlgorithmKnuth–Morris–Pratt AlgorithmBoyer–Moore String-Search Algorithm

Locate the Routing Tag

Tracked in this browser only
Write code

Trains the technique from

LeetCode 28Find the Index of the First Occurrence in a String

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.

Every crate in a depot is stencilled with a long identifier, given as the lowercase string label. A conveyor belt decides where a crate goes by hunting for a short routing marker inside that identifier, given as the lowercase string tag.

Return the smallest index i such that the len(tag) consecutive characters of label starting at position i spell tag exactly. If the marker is nowhere inside the identifier, return -1.

Both strings are non-empty, and the marker may well be longer than the identifier.

Examples

Example 1

Input
label = "abracadabra", tag = "cad"
Output
4

Characters 4 through 6 of the identifier spell the marker; nothing earlier does.

Example 2

Input
label = "mississippi", tag = "issip"
Output
4

A shorter partial match begins at position 1 and dies at the fourth character, so the answer is the later position 4 where all five characters line up.

Example 3

Input
label = "banana", tag = "nab"
Output
-1

The three characters never sit next to each other in that arrangement.

Constraints

  • 1 <= label.length <= 10^4
  • 1 <= tag.length <= 10^4
  • label and tag contain only lowercase English letters

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 locate_tag(label: str, tag: str) -> int:
Java
public int locateTag(String label, String tag)
September 7
Apply