Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 7051a28

Browse files
committed
500. Keyboard Row
1 parent 88027b5 commit 7051a28

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

500. Keyboard Row.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 500. Keyboard Row
2+
3+
### 2017-03-12
4+
5+
Given a List of words, return the words that can be typed using letters of **alphabet** on only one row's of American keyboard like the image below.
6+
7+
![American keyboard](https://leetcode.com/static/images/problemset/keyboard.png)
8+
9+
**Example 1:**
10+
11+
```
12+
Input: ["Hello", "Alaska", "Dad", "Peace"]
13+
Output: ["Alaska", "Dad"]
14+
15+
```
16+
17+
**Note:**
18+
19+
1. You may use one character in the keyboard more than once.
20+
2. You may assume the input string will only contain letters of alphabet.
21+
22+
23+
24+
# Solution
25+
26+
```swift
27+
class Solution {
28+
func findWords(_ words: [String]) -> [String] {
29+
var res = [String]()
30+
let row = ["[qwertyuiop]", "[asdfghjkl]", "[zxcvbnm]"].flatMap({
31+
try? RegularExpression(pattern: "^[\($0)]{0,}$", options: [.caseInsensitive])
32+
})
33+
for str in words {
34+
let range = NSRange.init(location: 0, length: str.utf8.count)
35+
for regx in row {
36+
if regx.matches(in: str, options: [], range: range).count > 0 {
37+
res.append(str)
38+
break
39+
}
40+
}
41+
}
42+
return res
43+
}
44+
}
45+
```
46+

0 commit comments

Comments
 (0)