website/content/ChapterFour/0500~0599/0500.Keyboard-Row.md
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.
Example:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如上图所示。
package leetcode
import "strings"
func findWords500(words []string) []string {
rows := []string{"qwertyuiop", "asdfghjkl", "zxcvbnm"}
output := make([]string, 0)
for _, s := range words {
if len(s) == 0 {
continue
}
lowerS := strings.ToLower(s)
oneRow := false
for _, r := range rows {
if strings.ContainsAny(lowerS, r) {
oneRow = !oneRow
if !oneRow {
break
}
}
}
if oneRow {
output = append(output, s)
}
}
return output
}