[C#][LeetCode][Easy] 500. Keyboard Row

心得

鍵盤共有三行[qwertyuiop, asdfghjkl, zxcvbnm],找出在同行字母拼湊出來的單字。

問題

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.

keyboard

 

Example 1:

1
2
3
4
5
6

Input: ["Hello", "Alaska", "Dad", "Peace"]
>
> Output: ["Alaska", "Dad"]
>
>

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14

public class Solution {
public string[] FindWords(string[] words) {
string row_1 = "qwertyuiop";
string row_2 = "asdfghjkl";
string row_3 = "zxcvbnm";

return words.Where(x =>
{
var str = x.ToLower().ToArray();
return !str.Any(y => !row_1.Contains(y)) || !str.Any(y => !row_2.Contains(y)) || !str.Any(y => !row_3.Contains(y));
}).ToArray();
}
}