-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path500_Keyboard_Row.js
More file actions
31 lines (27 loc) · 835 Bytes
/
500_Keyboard_Row.js
File metadata and controls
31 lines (27 loc) · 835 Bytes
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
/*
500. Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard.
*/
const expect = require('expect');
describe('500 Keyboard Row', () => {
it('returns ', () => {
//arrange
const input = ['Hello', 'Alaska', 'Dad', 'Peace'];
const expected = ['Alaska', 'Dad'];
//act
const actual = findWords(input);
//assert
expect(expected).toEqual(actual);
});
});
const findWords = (words) => {
const result = [];
const keyboardRows = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'];
words.forEach(word => {
const wordSet = new Set(word.toLowerCase().split(''));
for (const keyboardRow of keyboardRows) {
if ([...wordSet].every(char => keyboardRow.includes(char))) return result.push(word);
}
});
return result;
};