500. Keyboard Row - Easy
문제
Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
In the American keyboard:
- the first row consists of the characters “qwertyuiop”,
- the second row consists of the characters “asdfghjkl”, and
- the third row consists of the characters “zxcvbnm”.
제한사항
- 1 <= words.length <= 20
- 1 <= words[i].length <= 100
- words[i] consists of English letters (both lowercase and uppercase).
입출력 예
1
2
3
4
Example 1:
Input: words = ["Hello","Alaska","Dad","Peace"]
Output: ["Alaska","Dad"]
1
2
3
4
Example 2:
Input: words = ["omk"]
Output: []
1
2
3
4
Example 3:
Input: words = ["adsdf","sfd"]
Output: ["adsdf","sfd"]
풀이
- String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func search(keyboard string, word string) bool {
for _, c := range(word) {
if (!strings.Contains(keyboard, strings.ToLower(string(c)))) {
return false
}
}
return true
}
func findWords(words []string) []string {
const firstWord = "qwertyuiop"
const secondWord = "asdfghjkl"
const thirdWord = "zxcvbnm"
var result []string
for _, str := range(words) {
if (search(firstWord, str) || search(secondWord, str) || search(thirdWord, str)) {
result = append(result, str)
}
}
return result
}