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.
Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"]
Note:
static public string[] FindWords(string[] words) {
List<string> resultArr = new List<string>();
HashSet<char> line1 = new HashSet<char>() { ‘q‘, ‘w‘, ‘e‘, ‘r‘, ‘t‘, ‘y‘, ‘u‘, ‘i‘, ‘o‘, ‘p‘, ‘Q‘, ‘W‘, ‘E‘, ‘R‘, ‘T‘, ‘Y‘, ‘U‘, ‘I‘, ‘O‘, ‘P‘ };
HashSet<char> line2 = new HashSet<char>() { ‘a‘, ‘s‘, ‘d‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘l‘, ‘A‘, ‘S‘, ‘D‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘L‘ };
HashSet<char> line3 = new HashSet<char>() { ‘z‘, ‘x‘, ‘c‘, ‘v‘, ‘b‘, ‘n‘, ‘m‘, ‘Z‘, ‘X‘, ‘C‘, ‘V‘, ‘B‘, ‘N‘, ‘M‘ };
foreach (string str in words) {
if (Check(str, line3) || Check(str, line2) || Check(str, line1)) {
resultArr.Add(str);
}
}
return resultArr.ToArray();
}
static public bool Check(string str, HashSet<char> hashSet) {
foreach (char c in str) {
if (!hashSet.Contains(c)) {
return false;
}
}
return true;
}
500. 单词是否在键盘上的同一行 Keyboard Row
原文:http://www.cnblogs.com/xiejunzhao/p/7eba48d95dde2043b7402b1bf9257824.html