-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path500 Keyboard Row.js
54 lines (47 loc) · 1.32 KB
/
500 Keyboard Row.js
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
45
46
47
48
49
50
51
52
53
54
/**
* 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.
* 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。
* 键盘如下图所示。
* https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/keyboard.png
*/
/**
* Example 1:
* Input: ["Hello", "Alaska", "Dad", "Peace"]
* Output: ["Alaska", "Dad"]
*/
const checkLine = (line, _words, result) => {
let flag = true
console.log(line, _words)
_words.forEach((char) => {
if (!line.includes(char.toLowerCase())) {
flag = false
}
})
if (flag) {
result.push(_words.join(''))
}
}
/**
* @param {string[]} words
* @return {string[]}
*/
const findWords = (words) => {
let res = []
let line1 = [...'qwertyuiop']
let line2 = [...'asdfghjkl']
let line3 = [...'zxcvbnm']
words.forEach((word) => {
let wordArr = [...word]
if (line1.includes(wordArr[0].toLowerCase())) {
checkLine(line1, wordArr, res)
} else if (line2.includes(wordArr[0].toLowerCase())) {
checkLine(line2, wordArr, res)
} else if (line3.includes(wordArr[0].toLowerCase())) {
checkLine(line3, wordArr, res)
}
})
return res
}
console.log(findWords(['Hello', 'Alaska', 'Dad', 'Peace']))