LeetCode #500 Keyboard Row 键盘行

500 Keyboard Row 键盘行

Description:
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.

keyboard

Example:

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.

题目描述:
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。

键盘

示例:

输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]

注意:

你可以重复使用键盘上同一字符。
你可以假设输入的字符串将只包含字母。

思路:

存下三行键盘的值, 依次判断单词即可
时间复杂度O(mn), 空间复杂度O(1), m单词平均长度, n为数组中单词数

代码:
C++:

class Solution 
{
public:
    vector<string> findWords(vector<string>& words) 
    {
        vector<string> result;
        string first = "qwertyuiop", second = "asdfghjkl", third = "zxcvbnm";
        for (auto word : words) if (check(word, first, second, third)) result.push_back(word);
        return result;
    }
private:
    bool check(string s, string first, string second, string third) 
    {
        int one = 0, two = 0, three = 0;
        for(int i = 0; i < s.size(); i++) 
        {
              if (first.find(tolower(s[i])) != string::npos) one++;
              else if (second.find(tolower(s[i])) != string::npos) two++;
              else if (third.find(tolower(s[i])) != string::npos) three++;
        }
        if (one == s.size() or two == s.size() or three == s.size()) return true;
        else return false;
    }
};

Java:

class Solution {
    public String[] findWords(String[] words) {
        String first = "qwertyuiop";
        String second = "asdfghjkl";
        String third = "zxcvbnm";
        List<String> result = new ArrayList<>();
        for (String word : words) if (check(word.toLowerCase(), first, second, third)) result.add(word);
        return result.toArray(new String[result.size()]);
    }

    private boolean check(String word, String first, String second, String third) {
        int one = 0, two = 0, three = 0;
        for (int i = 0; i < word.length(); i++) {
            if (first.indexOf(word.charAt(i)) != -1) one++;
            if (second.indexOf(word.charAt(i)) != -1) two++;
            if (third.indexOf(word.charAt(i)) != -1) three++;
        }
        if (one == word.length() || two == word.length() || three == word.length()) return true;
        return false;
    }
}

Python:

class Solution:
    def findWords(self, words: List[str]) -> List[str]:
        return [word for word in words if any(all(c in row for c in word.lower()) for row in ["qwertyuiop", "asdfghjkl", "zxcvbnm"])]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容