URL: https://leetcode.com/problems/keyboard-row/description/ I write a function in a function. It's important to know the scope of the variable, and use print to test the simple code
class Solution:
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
# judge which row the input char belongs to
def judgeRow(c):
row1 =['q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P']
row2 =['a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L']
row3 =['z','x','c','v','b','n','m','Z','X','C','V','B','N','M']
if(row1.count(c)):
return 1
elif(row2.count(c)):
return 2
else:
return 3
listReturn = [] # to store the final retured list, outside the loop
for word in words:
wordRecover = ''
rowNum = judgeRow(word[0]) # judge the row from the first char
#print (rowNum)
for c in word:
rowCurrent = judgeRow(c)
print (rowCurrent)
if(rowCurrent != rowNum):
wordRecover = ''
break;
wordRecover += c
print (wordRecover)
if(wordRecover!=''):
listReturn.append(wordRecover)
return listReturn