LeetCode-500-Keyboard Row

来源:互联网 发布:广州哪里有淘宝美工学 编辑:程序博客网 时间:2024/05/22 11:47

https://leetcode.com/problems/keyboard-row/description/


#region 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:        //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 string[] FindWords(string[] words)        {            HashSet<string> hash_line1 = new HashSet<string>() { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p" };            HashSet<string> hash_line2 = new HashSet<string>() { "a", "s", "d", "f", "g", "h", "j", "k", "l" };            HashSet<string> hash_line3 = new HashSet<string>() { "z", "x", "c", "v", "b", "n", "m" };            List<string> list = new List<string>();            for (int i = 0; i < words.Length; i++)            {                if (IsInHashSet(hash_line1, words[i]) || IsInHashSet(hash_line2, words[i]) || IsInHashSet(hash_line3, words[i]))                {                    list.Add(words[i]);                }            }            return list.ToArray();        }        bool IsInHashSet(HashSet<string> hash, string strWord)        {            for (int i = 0; i < strWord.Length; i++)            {                if (!hash.Contains(strWord[i].ToString().ToLower()))                    return false;            }            return true;        }        #endregion


原创粉丝点击