[LeetCode]#1974. Minimum Time to Type Word Using Special Typewriter

  • Move the pointer one character counterclockwise or clockwise.
  • Type the character the pointer is currently on.
Input: word = "abc"
Output: 5
Explanation:
The characters are printed as follows:
- Type the character 'a' in 1 second since the pointer is initially on 'a'.
- Move the pointer clockwise to 'b' in 1 second.
- Type the character 'b' in 1 second.
- Move the pointer clockwise to 'c' in 1 second.
- Type the character 'c' in 1 second.
  1. if move <13, go clockwise. Else, go counterclockwise.
  2. Add one second for type.
  3. Summary all second
class Solution:
def minTimeToType(self, word):
s=0
now='a'
for i in range(len(word)):
move=abs(ord(word[i])-ord(now))
if move > 13:
move= 26 -move
s+=(move+1)
now=word[i]
return s

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Write a python program to simulate Elliptical Orbit.

Start Using Your Mac Like a Pro

A story of doing a personal software project (Part 1)

So long, but not goodbye

French Austrlian poet and systems administrator, Miguel Jacq.

Community AMA Transcript

Container Networking using Linux Namespaces — Part2 (Using Docker)

CS373 Spring 2022: Kristina Zhou

What exactly is a name? — Part II: Rehab

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#1967. Number of Strings That Appear as Substrings in Word

LeetCode Patterns Adventure 12 — Palindrome Linked List

4-in-1 Priority First Search in Python: BFS, DFS, Prim’s, and Dijkstra’s algorithms

Interview Tips: How to use Lists efficiently in Python