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.
American keyboard
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:
You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
解:
這一題開始時會覺得很難解, 但後來把每行鍵盤的英文字母分別放進字典
再把每個英文字用字典1,2,3來檢查
例如:Hello的e,o出現在字典1, row1c就變成1. Hello的h,l出現在字典2, row2c就變成1
Hello沒有字母出現在字典3 row3c 就依然是0
最後把3個row1c,row2c,row3c的數值加起來,
如果加起來是超過1的話就代表他出現在超過1行鍵盤上.
這樣就不是同一行Keyboard row 就不會把他印出來
class Solution:
def findWords(self, word):
row1={'q','w','e','r','t','y','u','i','o','p'};#放入字典1
row2={'a','s','d','f','g','h','j','k','l'};#放入字典2
row3={'z','x','c','v','b','n','m'};#放入字典3
newword=[]
for i in word:
row1c,row2c,row3c=0,0,0
count=0
for j in i:
if j in row1:
row1c=1
if j in row2:
row2c=1
if j in row3:
row3c=1
count=row1c+row2c+row3c
if count==1:
newword.append(i)
return newword
https://leetcode.com/problems/keyboard-row/description/