-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
1165 Single-Row Keyboard.swift
44 lines (35 loc) · 1.12 KB
/
1165 Single-Row Keyboard.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// 1165 Single-Row Keyboard.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 12/03/2021.
// Copyright © 2021 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/single-row-keyboard/
class Solution {
/// Finds how much time it takes to type `word` with one finger.
///
/// - Parameters:
/// - keyboard: The string.
/// - word: The word.
/// - Returns: How much time it takes to type `word` with one finger.
///
/// - Complexity:
/// - time: O(max(n, m)), where n is the length of `keyboard`, and m is
/// the length of `word`.
/// - space: O(n), where n is the length of `keyboard`.
func calculateTime(_ keyboard: String, _ word: String) -> Int {
var dict = [Character: Int]()
for (i, key) in keyboard.enumerated() {
dict[key] = i
}
var prev = 0
var ans = 0
for ch in word {
ans += abs(prev - (dict[ch] ?? 0))
prev = dict[ch] ?? 0
}
return ans
}
}