500.键盘行
链接:500.键盘行
难度:Easy
标签:数组、哈希表、字符串
简介:给你一个字符串数组 words ,只返回可以使用在 美式键盘 同一行的字母打印出来的单词。
题解 1 - typescript
- 编辑时间:2021-10-31
- 执行用时:76ms
- 内存消耗:39.3MB
- 编程语言:typescript
- 解法介绍:哈希。
function findWords(words: string[]): string[] {
const data: Set<string>[] = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'].map(
str => new Set<string>(str.split(''))
);
return words.filter(word => {
word = word.toLowerCase();
const set = data.find(set => set.has(word[0]))!;
for (const ch of word) {
if (!set.has(ch)) return false;
}
return true;
});
}