There are three rows in a keyboard containing the alphabets. The alphabets in each row are given as the input. The program also accepts a string S which is typed using the keyboard. The program must print the row in which the most number of alphabets typed continuously. If there are two or more such tow then the program must print them in the order of their occurrence. If there is no such cow, then the program must print -1 as the output
Boundary Condition(s): 1<= Length of S<= 1000
Input Format:
The first three lines contain the three rows of the keyboard.
The fourth line contains S
Output Format:
The lines containing the string values or the first line contains-1 based on the given conditions.
Example Input/Output 1:
Input:
qwertyuiop
asdfghjkl
zxcvbnm
skilrack
Output:
asdfghjkl
Explanation:
Here S = skillrack
Two alphabets are typed continuously in the middle raw (2)
s and k
l and l
Example Input/Output 2:
Input:
qwertyuiop
asdfghjkl
zxcvbnm
motion
Outpart:
qwertyuiop
Example Input/Output 3:
Input:
qwertyuiop
asdfghjkl
zxcvbnm
ice
Output:
-1
Example Input/Output 4:
Input:
qwertyuiop
asdfghjkl
zxcvbnm
google
Output:
qwertyuiop
asdfghjkl
Solution:
def getRow(ch):for i in range(3):if(ch in keys[i]):return ikeys=["qwertyuiop","asdfghjkl","zxcvbnm"]s="skillrack"keyIndex,ind,count,prevMaxCount=[],getRow(s[0]),1,1for i in range(1,len(s)):if(getRow(s[i])==getRow(s[i-1])):ind,count=getRow(s[i]),count+1else:if(count>prevMaxCount):keyIndex=[]keyIndex.append(ind)prevMaxCount=countelif prevMaxCount==count and count>1:keyIndex.append(ind)prevMaxCount=countcount,ind=1,getRow(s[-2])if(len(s)>1 and getRow(s[-1])==getRow(s[-2])):if(count>prevMaxCount):keyIndex=[]keyIndex.append(ind)elif prevMaxCount==count and count>1:keyIndex.append(ind)if(keyIndex):keyIndex=sorted(set(keyIndex))for ind in keyIndex:print(keys[ind])else:print-1
0 Comments