Skip to the code
cf.
+10 XP

#616 Find a Snippet in a Bundle

easy

A bundle analyzer checks where a known code snippet first appears inside a minified bundle.

Write firstIndex(bundle, snippet) that returns the smallest index where snippet starts in bundle, or -1 if it doesn't appear. Don't use indexOf, includes, or regular expressions: compare windows yourself.

Example 1

Input: bundle = "var a=1;function f(){return a}", snippet = "function"
Output: 8

function starts right after var a=1;.

Example 2

Input: bundle = "abc", snippet = "d"
Output: -1

d never appears.

Constraints

  • 0 <= bundle.length <= 10^5
  • 1 <= snippet.length <= 10^3
  • Both contain printable ASCII characters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).