Skip to content

[500] Keyboard Row

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

  • algorithms
  • Easy (60.96%)
  • Source Code: 500.keyboard-row.py
  • Total Accepted: 85.8K
  • Total Submissions: 138.5K
  • Testcase Example: '["Hello","Alaska","Dad","Peace"]'

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"] 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.
python
class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        result = []
        if not words: return result

        for w in words:
            if self.isvalid(w.lower()): result.append(w)

        return result

    def isvalid(self, w):
        keymap = [
            set(list('qwertyuiop')),
            set(list('asdfghjkl')),
            set(list('zxcvbnm'))
        ]
        return True if any(map(lambda x: set(list(w)).issubset(x), keymap)) else False

Last updated: