-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path500_KeyboardRow.swift
More file actions
43 lines (41 loc) · 1.17 KB
/
500_KeyboardRow.swift
File metadata and controls
43 lines (41 loc) · 1.17 KB
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
class Solution {
let row1 = "qwertyuiopQWERTYUIOP"
let row2 = "asdfghjklASDFGHJKL"
let row3 = "zxcvbnm(ZXCVBNM)"
private func row(of letter: String) -> Int {
if row1.contains(letter) {
return 1
} else if row2.contains(letter) {
return 2
} else if row3.contains(letter) {
return 3
} else {
return 0
}
}
private func canTypeInRow(_ word: String) -> Bool {
let firstLetter = String(word.first!)
let firstLetterRow = row(of: firstLetter)
let startIndex = word.index(after: word.startIndex)
for item in word[startIndex...] {
let letter = String(item)
let row = self.row(of: letter)
if row != firstLetterRow || row == 0 {
return false
}
}
return true
}
func findWords(_ words: [String]) -> [String] {
var results = [String]()
for word in words {
if word.isEmpty {
continue
}
if canTypeInRow(word) {
results.append(word)
}
}
return results
}
}