心得
鍵盤共有三行[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.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"]Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
答案
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();
}
}
