Last active
February 12, 2019 20:54
-
-
Save bcowell/a82dbb1256524d5a9dd391896a13acd7 to your computer and use it in GitHub Desktop.
shortest-to-char
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution for leetcode 821. Shortest Distance to a Character