LeetCode-500-Keyboard Row

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


发布了14 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/m0_37302219/article/details/78284002