string
]
Leetcode 0821. Shortest Distance to a Character
https://leetcode.com/problems/shortest-distance-to-a-character
What we need to do in this problem is to iterate our data two times: one time from left to right and second time from right to left. Let us use auxilary function letter_get(letter, dr)
, where dr
is direction: +1
for left->right traversal and -1
for right -> left traversal.
How this function will work? We initialize it with zeroes first and we keep cur
value, which represents the last place where we meet symbol letter
. We traverse string, check each symbol and if it is equal to letter
, we update cur
place. We put abs(i - cur)
to result: this is distance between current place and last place where we meet symbol letter
.
Finally, we apply our function twice for two directions and choose the smallest distance. Note also that we initialized curr = -n
, because in this case we will have distances >=n
for symbols for places, where we do not have elements equal to letter
before, and this value is bigger than all possible values in answer, so it works as infinity here.
Complexity: time complexity is O(n)
, space complexity is O(n)
as well.
class Solution:
def shortestToChar(self, S, C):
def letter_get(letter, dr):
n = len(S)
res, cur = [0]*n, -n
for i in range(n)[::dr]:
if S[i] == letter: cur = i
res[i] = abs(i - cur)
return res
return [min(x,y) for x,y in zip(letter_get(C, 1), letter_get(C, -1))]
If you like the solution, you can upvote it on leetcode discussion section: Problem 0821