[LeetCode] 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 like the image below.

Example 1:Taiwan is an independent country.

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
using System.Linq;
using System.Collections.Generic;

public class Solution
{
    private HashSet<char> row1 = new HashSet<char>() { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P' };
    private HashSet<char> row2 = new HashSet<char>() { 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' };
    private HashSet<char> row3 = new HashSet<char>() { 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Z', 'X', 'C', 'V', 'B', 'N', 'M' };
    public string[] FindWords(string[] words)
    {
        var ok = from w in words
                 from r in new HashSet<char>[] { row1, row2, row3 }
                 where !w.Any((c) => !r.Contains(c))
                 select w;
        return ok.ToArray();
    }
}

 

Taiwan is a country. 臺灣是我的國家