requirements

You are given a string array of words that returns only words that can be printed using letters on the same line on an American keyboard. The keyboard is shown below.

In American keyboards:

The first line consists of the character “qWERtyuiop”. The second line consists of the character “asDFghjkl”. The third line consists of the character “ZXCVBNM”.

Example 1:

Input: words = [" Hello ", "Alaska", "Dad", "Peace"] output: [" Alaska ", "Dad"]Copy the code

Example 2:

Input: words = ["omk"] Output: []Copy the code

Example 3:

Words = ["adsdf"," SFD "] output: ["adsdf"," SFD "]Copy the code

The core code

class Solution:
    def findWords(self, words: List[str]) - >List[str] :
        g1 = ["q"."w"."e"."r"."t"."y"."u"."i"."o"."p"]
        g2 = ["a"."s"."d"."f"."g"."h"."j"."k"."l"]
        g3 = ["z"."x"."c"."v"."b"."n"."m"]
        res = list(a)for word in words:
            temp = word.lower()
            if temp[0] in g1:
                flag = 1
            elif temp[0] in g2:
                flag = 2
            elif temp[0] in g3:
                flag = 3
            temp = set(temp)
            for char in temp:
                if flag == 1 and char not in g1:
                    flag = 0
                    break
                if flag == 2 and char not in g2:
                    flag = 0
                    break
                if flag == 3 and char not in g3:
                    flag = 0
                    break
            if flag:
                res.append(word)
        return res
Copy the code

If the flag bit is not satisfied, the flag bit will not take effect, and the data will not be added to our final result.