Skip to the code
cf.
+10 XP

#597 Distance to Nearest Marker

easy

A code editor shows faint guides that fade with distance from the nearest bookmark. A line of text is a string line, and bookmarks are every position holding the character mark.

Write distanceToMark(line, mark) that returns an array where item i is the distance Math.abs(i - j) from position i to the closest position j with line[j] === mark. mark appears in line at least once.

Example 1

Input: line = "placeholder", mark = "e"
Output: [4,3,2,1,0,1,2,2,1,0,1]

For example position 0 is 4 away from the e at position 4.

Example 2

Input: line = "aaab", mark = "b"
Output: [3,2,1,0]

The only marker is at the end.

Constraints

  • 1 <= line.length <= 100000
  • mark is a single character that appears in line.
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).