Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance to a Character

Try to solve the Shortest Distance to a Character problem.

Statement

Given a string s and a character c that occurs in s, return an array of integers answer where answer.length == s.length and answer[i] is the minimum distance from index i to any index j such that s[j] == c.

The distance between two indices i and j is defined as ∣i−j∣|i - j|.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s[i] and c are lowercase English letters.

  • It is guaranteed that c occurs at least once in s.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance to a Character

Try to solve the Shortest Distance to a Character problem.

Statement

Given a string s and a character c that occurs in s, return an array of integers answer where answer.length == s.length and answer[i] is the minimum distance from index i to any index j such that s[j] == c.

The distance between two indices i and j is defined as ∣i−j∣|i - j|.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s[i] and c are lowercase English letters.

  • It is guaranteed that c occurs at least once in s.