forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshift-distance-between-two-strings.py
More file actions
30 lines (29 loc) · 1.01 KB
/
shift-distance-between-two-strings.py
File metadata and controls
30 lines (29 loc) · 1.01 KB
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
# Time: O(n + 26)
# Space: O(26)
# prefix sum
class Solution(object):
def shiftDistance(self, s, t, nextCost, previousCost):
"""
:type s: str
:type t: str
:type nextCost: List[int]
:type previousCost: List[int]
:rtype: int
"""
prefix1 = [0]*(len(nextCost)+1)
for i in xrange(len(nextCost)):
prefix1[i+1] = prefix1[i]+nextCost[i]
prefix2 = [0]*(len(previousCost)+1)
for i in xrange(len(previousCost)):
prefix2[i+1] = prefix2[i]+previousCost[i]
result = 0
for i in xrange(len(s)):
if s[i] == t[i]:
continue
left = ord(s[i])-ord('a')
right = ord(t[i])-ord('a')
if left <= right:
result += min(prefix1[right]-prefix1[left], prefix2[-1]-(prefix2[right+1]-prefix2[left+1]))
else:
result += min(prefix2[left+1]-prefix2[right+1], prefix1[-1]-(prefix1[left]-prefix1[right]))
return result