python code using dictionary


  • 0
    L

    Here a code using dictionary.

    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        keyb = {}
        keyb['Q'] = 1
        keyb['W'] = 1
        keyb['E'] = 1
        keyb['R'] = 1
        keyb['T'] = 1
        keyb['Y'] = 1
        keyb['U'] = 1
        keyb['I'] = 1
        keyb['O'] = 1
        keyb['P'] = 1
        
        keyb['A'] = 2
        keyb['S'] = 2
        keyb['D'] = 2
        keyb['F'] = 2
        keyb['G'] = 2
        keyb['H'] = 2
        keyb['J'] = 2
        keyb['K'] = 2
        keyb['L'] = 2
        
        keyb['Z'] = 3
        keyb['X'] = 3
        keyb['C'] = 3
        keyb['V'] = 3
        keyb['B'] = 3
        keyb['N'] = 3
        keyb['M'] = 3
        for word in words[:]:
            row = keyb[word[0].upper()]
            for letter in word:
                if keyb[letter.upper()] != row:
                    words.remove(word)
                    break
        return words
                '''

  • 0
    E

    Hey, my answer was similar to this but I do not understand the functional difference between the following two statements.
    for the outer loop why does "for word in words[:]:" work while "for word in words:" does not?


Log in to reply
 

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