https://leetcode.com/problems/shortest-distance-to-a-character/
import math
class Solution:
def shortestToChar(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
n = len(S)
res = [n]*n
pos = S.index(C)
for i in range(n):
if S[i] == C:
pos=i
res[i] = abs(pos-i)
S1 = S[::-1]
pos = S1.index(C)
for i in range(n):
if S1[i] ==C:
pos=i
res[n-i-1] = min(res[n-i-1],abs(pos-i))
return res