5 views
# Shortest Distance to a Character (LeetCode 821) — Interview Notes For each index `i`, compute distance to the nearest `c` in `s`. Two-pass trick: ```text prev = -inf left-to-right: ans[i] = i - prev when s[i]==c update prev prev = +inf right-to-left: ans[i] = min(ans[i], prev - i) ``` O(n) time, O(1) extra beyond output. Mention why two passes beat nested scans. Affiliation: [Linkjob.ai](https://www.linkjob.ai/) interview practice notes.