func calculateTime(_ keyboard: String, _ word: String) -> Int {
var ar = Array(repeating: 0, count: 26)
for (i, char) in keyboard.enumerated() {
ar[Int(char.asciiValue!) - 97] = i
}
var previous = 0
var time = 0
for char in word {
let index = ar[Int(char.asciiValue!) - 97]
time += abs(index - previous)
previous = index
}
return time
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme