-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path821.py
48 lines (36 loc) · 1.47 KB
/
821.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
__________________________________________________________________________________________________
sample 36 ms submission
class Solution:
def shortestToChar(self, S: str, C: str) -> List[int]:
cs = [-len(S)] + [i for i in range(len(S)) if S[i]==C] + [2*len(S)]
idx = 0
res = []
for i in range(len(S)):
val = min(i-cs[idx], cs[idx+1]-i)
if val==0:
idx+=1
res.append(val)
return res
__________________________________________________________________________________________________
sample 13028 kb submission
class Solution:
def shortestToChar(self, S: str, C: str) -> List[int]:
ePosition = []
result = []
length = len(S)
counter = 0
for i in range(length):
if S[i] == C:
ePosition.append(i)
eCounts = len(ePosition)
for i in range(length):
if counter == eCounts:
result.append(abs(ePosition[counter - 1] - i))
else:
left = abs(ePosition[counter- 1] - i)
right = abs(ePosition[counter] - i)
result.append(min(left, right))
if i >= ePosition[counter]:
counter += 1
return result
__________________________________________________________________________________________________