leetcode算法: 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.

American keyboard

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.

这道题描述的是:给定一些单词,让我们找出这些单词中哪些单词是 所有字母都在键盘同一行上

qwertyuiop是键盘的第一行asdfghjkl是键盘的第二行zxcvbnm是键盘第三行

刚拿到这道题还真的很头疼- -难道要对单词每个字母进行遍历吗??

后来参考了其他大神的思想,大致是两种思路,一种是正则表达式匹配,另一种是集合思想。我用了集合的思想。用三个集合分别存下键盘个行的所有字母。当给定一个单词 我们看看这个单词是不是这三个集合某一个的子集,如果是,那这个单词就满足字母都在一行

我的代码:
 1 class Solution(object):
 2     def findWords(self, words):
 3         """
 4         :type words: List[str]
 5         :rtype: List[str]
 6         """
 7         q,a,z = set("qwertyuiop"), set("asdfghjkl"), set("zxcvbnm")
 8         res = []
 9         for str in words:
10             lset = set(str.lower() )
11             if lset.issubset(q) or lset.issubset(a) or lset.issubset(z):
12                 res.append(str)
13         return res
14
15
16
17 if __name__ == ‘__main__‘:
18     s = Solution()
19     res = s.findWords(["Hello", "Alaska", "Dad", "Peace"])
20     print(res)
时间: 2024-08-07 10:54:17

leetcode算法: Keyboard Row的相关文章

Leetcode#500. Keyboard Row(键盘行)

题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 注意: 你可以重复使用键盘上同一字符. 你可以假设输入的字符串将只包含字母. 思路 把键盘中的字母和其所在行数放到map中,然后比较一个字符串中是否都来自一行.

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: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["A

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: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["A

LeetCode算法题-Keyboard Row(Java实现)

这是悦乐书的第245次更新,第258篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第112题(顺位题号是500).给定一个单词列表,返回可以在美国键盘的一行上使用字母表键入的单词,如下图所示. 例如: 输入:["Hello","Alaska","Dad","Peace"] 输出:["Alaska","Dad"] 注意: 您可以多次使用键盘中的一个字符. 您可

Keyboard Row

1. Title500. Keyboard Row2. Http addresshttps://leetcode.com/problems/keyboard-row/?tab=Description3. The question 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 ima

leetcode算法: Find Bottom Left Tree Value

leetcode算法: Find Bottom Left Tree Value Given a binary tree, find the leftmost value in the last row of the tree. Example 1:Input: 2 / \ 1 3 Output:1Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output:7Note: You may assume the tree (i.e., the given ro

LeetCode_500. Keyboard Row

500. Keyboard Row Easy 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: Input: ["Hello", "Alaska", "Dad", "Peace&qu

LeetCode算法编程(两题)

今天看到酷壳推荐的国外编程LeetCode算法编程网站,上面目前有154道算法题,感觉很有意思,平常工作也比较忙,现在很少有时间来锻炼算法相关的东西,有空的时候静下心来,温习下基础,活跃下自已的思路,也是有必要的.先做了几道,后面会陆续补充其它的题目. 1.题目-PlusOne Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored s

Leetcode 算法题--ReverseWordsInString

翻转字符串,想到什么写什么...我的做法是先trim掉空格,然后从字符串尾部开始扫描,遇到空格则认为一个单词结束,然后copy这个单词.需要注意的地方在于当扫描到最后一个单词的第一个字母时(譬如the sky is blue的t字母),注意单词长度的自增逻辑. 网上还有人的做法是反转整个字符串,然后逐个翻转单词. 1 package edu.hust.sse.Problems; 2 3 //Given s = "the sky is blue", 4 //return "bl

leetcode 算法 之 马拉松算法(Manacher's algorithm)(未完成)

马拉松算法:马拉松算法是用来计算一个字符串中最长的回文字符串(对称字符串,如aba abba). 首先,我们拿到一个字符串S,然后在S中的每个字符之间加#.例如:S="abcb" T="a#b#c#b" 我们T字符串的每一个T[i]向延伸d个字符 使得 T[i-d,i+d]是一个回文字符串.你会立刻发现,d就是以T[i]为中心的最长回文字符串的长度. 我们建立一个P数组,是的P数组的长度等于T的长度,每一个P[i]的值表示对应的T[i]为中心的最大回文字符串的长度.