Minimum moves required to type a word in QWERTY based keyboard | Function that calculates the moves required to prthe current String ; row1 has qwertyuiop , row2 has asdfghjkl , and row3 has zxcvbnm Store the row number of each character ; String length ; Initialise move to 1 ; Traverse the String ; If current row number is not equal to previous row then increment the moves ; Return the moves ; Driver Code ; Given String str ; Function Call
def numberMoves ( s ) : NEW_LINE INDENT row = [ 2 , 3 , 3 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 3 , 1 , 3 , 1 , 3 ] ; NEW_LINE n = len ( s ) ; NEW_LINE move = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE if ( row [ ord ( s [ i ] ) - ord ( ' a ' ) ] != row [ ord ( s [ i - 1 ] ) - ord ( ' a ' ) ] ) : NEW_LINE move += 1 ; NEW_LINE return move ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " ; NEW_LINE print ( numberMoves ( str ) ) ; NEW_LINE DEDENT