Easy understand solution in 7 lines for everyone

  • 34

    First of all,

    if you are python user {
     please upvote if it makes sense;
    } else if you are C++/Java user {
     please let me know if somewhere is not clear;

    I have used set to check the word.
    I firstly make every line a set of letter.
    Then I check every word if this word set is the subset if any line set.

    def findWords(self, words):
        line1, line2, line3 = set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')
        ret = []
        for word in words:
          w = set(word.lower())
          if w.issubset(line1) or w.issubset(line2) or w.issubset(line3):
        return ret

  • 2

    Haha, there is a bug, your else if doesn't have condition.

    Here is a python version :)

    use_python, make_sense, up_vote = True, True, 0
    if use_python and make_sense:
        up_vote += 9999
        raise NotClearException

  • 0

    @zhongyuan9817 Thanks your are right. As a python user, I hear a lot complaint from cpp and java. I want to make python be known as an easy understood language :)

  • 1

    Thanks ,@lee215 . I got the same solution with you. But I used filter(fun, iterable) to get the return list. : )

  • 0
    This post is deleted!

  • 0

    Same idea but "some what" one-liner XD:

    def findWords(self, words):
        return filter(lambda word:
                            set(word.lower()) - set("qwertyuiop") == set() or
                            set(word.lower()) - set("asdfghjkl") == set() or
                            set(word.lower()) - set("zxcvbnm") == set(),

  • 0

    Slightly different:

        def findWords(self, words):
            :type words: List[str]
            :rtype: List[str]
            keyboards = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm']
            ans = []
            for w in words:
                if any(set(w.lower()) <= set(r) for r in keyboards):
            return ans

Log in to reply

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