Python solution


  • 0
    D
    class Solution(object):
        def findWords(self, words):
            """
            :type words: List[str]
            :rtype: List[str]
            """
            
            def buildLetterToRowMap():
                rows = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
                return { char: i for i in range(len(rows)) for char in rows[i] }
            
            letterToRowMap = buildLetterToRowMap()
    
            def areCharsOnSameRow(word):
                rowOfFirstChar = letterToRowMap[word[0]]
                for char in word:
                    if letterToRowMap[char] != rowOfFirstChar:
                        return False
                return True
            
            return [ word for word in words if areCharsOnSameRow(word.lower()) ]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.