Notes
Notes - notes.io |
Jakub is trying out a one-dimensional keyboard. It consists of a single row of keys. The distance between any two adjacent keys is 1. Each key contains a distinct letter of the English alphabet. Jakub uses only one finger to type on the keyboard. He wonders what is the smallest total distance he will have to move his finger while typing a given word.
For example, if the keyboard's only row is "qwertyuiop", and Jakub wants to type the word "potter", he will have to move his finger from 'p' to 'o' (distance 1), from 'o' to 't' (distance 4), from 't' to 't' (distance 0), from 't' to 'e' (distance 2) and from 'e' to 'r' (distance 1), for a total distance of 1 + 4 + 0 + 2 + 1 = 8.
You are given a String keyboard and a String word, describing the keyboard and the word Jakub wants to write. Return the minimum distance he will have to move his finger in order to type the word on the keyboard.
Definition
Class:
TypingDistance
Method:
minDistance
Parameters:
String, String
Returns:
int
Method signature:
int minDistance(String keyboard, String word)
(be sure your method is public)
Notes
-
When moving the finger from the i-th key to the j-th key, the distance covered by the move can be computed as |i-j|, that is, the positive difference between i and j.
Constraints
-
keyboard will contain between 1 and 26 characters, inclusive.
-
Each character in keyboard will be a different lowercase letter of the English alphabet ('a'-'z').
-
word will contain between 1 and 50 characters, inclusive.
-
Each character in word will be present in keyboard.
Examples
0)
"qwertyuiop"
"potter"
Returns: 8
The example from the problem statement.
1)
"tc"
"tctcttccctccccttc"
Returns: 9
2)
"a"
"aaaaaaaaaaa"
Returns: 0
3)
"kwadrutove"
"rowerowe"
Returns: 39
4)
"qwertyuiopasdfghjklzxcvbnm"
"topcodersingleroundmatchgoodluckhavefun"
Returns: 322
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team