Shortest Distance to a Character Problem
Shortest Distance to a Character Problem — ExecCode Easy DSA Practice
Solve the Shortest Distance to a Character problem on ExecCode. Free online easy DSA practice in Arrays - Logic Building. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Description You are 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 distance from index i to the closest occurrence of character c in s. Your task is to the distance between two indices i and j is abs(i - j), where abs is the absolute value function. Return the required answer exactly as shown in the examples.
Examples
Input {"s": "loveleetcode", "c": "e"}; Output [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]. Input {"s": "aaab", "c": "b"}; Output [3, 2, 1, 0]
Constraints
1 <= s.length <= 10^4 s[i] and c are lowercase English letters. It is guaranteed that c occurs at least once in s.
Practice Shortest Distance to a Character free on ExecCode. Browse DSA problems, topic map, and placement guides.