# current pointer
cur = 'a'
for c in word:
if cur != c:
# there are 26 letters from a - z
# using built-in ord() func to calculate the ASCII value of each letter
# no negative values -> use absolute function
# clockwise
clockwise_cost = abs(ord(cur) - ord(c))
# counter-clockwise
# going counter-clockwise we just need to subtract the clockwise_cost with the number of letters from a-z
counter_cost = abs(clockwise_cost - 26)
if clockwise_cost < counter_cost:
res += clockwise_cost
else:
res += counter_cost
# remember to update the current pointer position
cur = c
# type a char
res += 1
return res