[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

8 Best Mobile App Development Programming Languages

Creating N1QL Labelled Image Database using Couchbase, FastAPI, and SerpApi

C# — Is collection empty — Count vs Any?

Dependency Injection and Testing in JUnit 5

The Walrus Operator in Python 3.8: A Primer

Hearts spread out on floor

Democratise all your Assets in the Data Lake with FastAPI and Cassandra

Data Flow Architcture of the Data API

Como Ativar O Omnisphere 2

Part III: Redemption — Return of the Orchestrator

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 Patterns Adventure 16 — Merge Two Sorted Lists

LeetCode has a Medium coding Problem in Its’ Algorithm Section “Regular Expression Matching…

Basic Data Structure and Algorithms in Python

A SHED UNDER THE TREE DATA STRUCTURE