0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Leetcode #1165: Single-Row Keyboard

Posted at
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
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?