Python Sol: Hashmap


  • 0
    J

    Just us a hashmap:

    class Solution:
        def findWords(self, words):
            """
            :type words: List[str]
            :rtype: List[str]
            """
            dic = {'q':1,'w':1,'e':1,'r':1,'t':1,'y':1,'u':1,'i':1,'o':1,'p':1,
                   'a':2,'s':2,'d':2,'f':2,'g':2,'h':2,'j':2,'k':2,'l':2,
                   'z':3,'x':3,'c':3,'v':3,'b':3,'n':3,'m':3,
                  }
            ans = list()
            for word in words:
                count = 0
                curr = 0
                for i in range(len(word)):
                    if dic.get(word[i].lower()) != curr:
                        count += 1
                        curr = dic.get(word[i].lower())
                        if count == 2:
                            break
                if count == 1:
                    ans.append(word)
                    
            return ans
    

Log in to reply
 

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