leetcode 500 keyboard row

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below

Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]


Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.


class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
       
        length = len(words)
       
        memo = []
        for iz in range(1,length+1):
            memo.append(0)
        result = []
        str1 = ['Q','W','E','R','Y','T','U','I','O','P','q','w','e','r','t','y','u','i','o','p']
        str2 = ['A','S','D','F','G','H','J','K','L','a','s','d','f','g','h','j','k','l']
        str3 = ['Z','X','C','V','B','N','M','z','x','c','v','b','n','m']
       
        iy1 = 0
        iy3 = 0
       
        iy5 = 0
        ix7 = 0
        i = 0
        for iy in range(1,length+1):
            ix1 = 0
            for ix in range(1,21):
                if str1[ix1] in words[iy1]:
                    memo[iy1] = memo[iy1] +1
                    break
                ix1 = ix1 +1
            iy1 = iy1 + 1
           
           
        for iy2 in range(1,length+1):
            ix3 = 0
            print('iy3',iy3)
       
          
            for ix2 in range(1,19):
                if str2[ix3] in words[iy3]:
                    memo[iy3] = memo[iy3] + 1
                    print('miy1',memo[iy3])
                    break
                ix3 = ix3 +1
            iy3 = iy3 + 1
           
        for iy4 in range(1,length+1):
            ix5 = 0
            for ix4 in range(1,15):
                 if str3[ix5] in words[iy5]:
                    memo[iy5] = memo[iy5] + 1
                    break
                 ix5 = ix5 +1
            iy5 = iy5 + 1
            
        for ix6 in range(1,length+1):
             print('ix7',ix7)
             if memo[ix7] ==1:
                result.append(words[ix7])
               
                i = i + 1
             ix7 = ix7 +1
            
        return result


ps: python中的for循环 for x in range(1,20) 实际只循环19次!!

介绍集合set的操作

http://www.jb51.net/article/57214.htm

s.issubset(t) 
s <= t  # 测试是否 s 中的每一个元素都在 t 中 

def findWords(words):
        return [word for row in [set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')] for word in words if set(word.lower()) <= row]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值