Skip to content

Instantly share code, notes, and snippets.

@bcowell
Last active February 12, 2019 20:54
Show Gist options
  • Save bcowell/a82dbb1256524d5a9dd391896a13acd7 to your computer and use it in GitHub Desktop.
Save bcowell/a82dbb1256524d5a9dd391896a13acd7 to your computer and use it in GitHub Desktop.
shortest-to-char
def shortestToChar(self, S: 'str', C: 'str') -> 'List[int]':
# store index of all chars c
ind = []
for i,c in enumerate(S):
if (c == C):
ind.append(i)
# Find shortest distance
# from current i to indexed c
arr = []
for i,s in enumerate(S):
closestPos = min(ind, key=lambda x:abs(x-i))
distance = abs(closestPos-i)
arr.append(distance)
return arr
@bcowell
Copy link
Author

bcowell commented Feb 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment