Verbose python solution without using regex


  • 0
    P
    class Solution(object):
        def findWords(self, words):
            """
            :type words: List[str]
            :rtype: List[str]
            """
            def is_only_one_line(word):
                lines = [
                    ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
                    ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
                    ['z', 'x', 'c', 'v', 'b', 'n', 'm']
                ]
                line_count = 0
                for line in lines:
                    for c in word.lower():
                        if c in line:
                            line_count += 1
                            break
                    
                    if line_count > 1:
                        return False
                    
                return line_count == 1
                
            return [w for w in words if is_only_one_line(w)]
    

Log in to reply
 

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