Straightforward Python Solution


  • 0
    L

    Make sure that all the characters are in the same row by checking it iteratively.
    To understand the else after the inner for loop, please refer to python documentation.

    class Solution(object):
        def findWords(self, words):
            """
            :type words: List[str]
            :rtype: List[str]
            """
            def getRow(c):
                c = c.lower()
                if c in "qwertyuiop": return 1
                if c in "asdfghjkl": return 2
                if c in "zxcvbnm": return 3
            result = []
            for word in words:
                for i, c in enumerate(word):
                    if i==0: r = getRow(c)
                    if r!= getRow(c): break
                else:
                    result.append(word)
            return result
    

Log in to reply
 

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