Skip to content
Related Articles
Minimum moves required to type a word in QWERTY based keyboard
• Difficulty Level : Basic
• Last Updated : 26 Aug, 2020

Given a string S consisting of only lowercase English letters. The task is to find the minimum number of times of finger moves to type the given string. The move is considered when you press the key which is not in the row to currently pressed key on the keyboard. QWERTY keyboard consisting of only English letters.

Examples:

Input: S =geeksforgeeks”
Output: 7
Explanation:
move 1 >> g
move 2 >> e
move 2 >> e
move 3 >> k
move 3 >> s
move 3 >> f
move 4 >> o
move 4 >> r
move 5 >> g
move 6 >> e
move 6 >> e
move 7 >> k
move 7 >> s

Input: S = “radhamohan”
Output: 6

Approach: This can be done by initially setting the row number of each character in the QWERTY keyboard. Below are the steps:

1. Store the row of each character in an array row[].
2. Initialize move = 1.
3. Traverse the given string and check if the row of the current character in the string is equal to the previous character or not.
4. If current character is not equal then increment the move as we need to change the current row while printing the character.
5. Else check for the next pairs of characters.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function that calculates the moves``// required to print the current string``int` `numberMoves(string s)``{``    ``// row1 has qwertyuiop, row2 has``    ``// asdfghjkl, and row3 has zxcvbnm``    ``// Store the row number of``    ``// each character``    ``int` `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 };` `    ``// String length``    ``int` `n = s.length();` `    ``// Initialise move to 1``    ``int` `move = 1;` `    ``// Traverse the string``    ``for` `(``int` `i = 1; i < n; i++) {` `        ``// If current row number is``        ``// not equal to previous row``        ``// then increment the moves``        ``if` `(row[s[i] - ``'a'``]``            ``!= row[s[i - 1] - ``'a'``]) {``            ``move++;``        ``}``    ``}` `    ``// Return the moves``    ``return` `move;``}` `// Driver Code``int` `main()``{``    ``// Given String str``    ``string str = ``"geeksforgeeks"``;` `    ``// Function Call``    ``cout << numberMoves(str);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG{` `// Function that calculates the moves``// required to print the current String``static` `int` `numberMoves(String s)``{``    ``// row1 has qwertyuiop, row2 has``    ``// asdfghjkl, and row3 has zxcvbnm``    ``// Store the row number of``    ``// each character``    ``int` `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` `};` `    ``// String length``    ``int` `n = s.length();` `    ``// Initialise move to 1``    ``int` `move = ``1``;` `    ``// Traverse the String``    ``for` `(``int` `i = ``1``; i < n; i++)``    ``{` `        ``// If current row number is``        ``// not equal to previous row``        ``// then increment the moves``        ``if` `(row[s.charAt(i) - ``'a'``] !=``            ``row[s.charAt(i-``1``) - ``'a'``])``        ``{``            ``move++;``        ``}``    ``}` `    ``// Return the moves``    ``return` `move;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``// Given String str``    ``String str = ``"geeksforgeeks"``;` `    ``// Function Call``    ``System.out.print(numberMoves(str));``}``}` `// This code is contributed by sapnasingh4991`

## Python3

 `# Python3 program for the above approach` `# Function that calculates the moves``# required to prthe current String``def` `numberMoves(s):``  ` `    ``# row1 has qwertyuiop, row2 has``    ``# asdfghjkl, and row3 has zxcvbnm``    ``# Store the row number of``    ``# each character``    ``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``];` `    ``# String length``    ``n ``=` `len``(s);` `    ``# Initialise move to 1``    ``move ``=` `1``;` `    ``# Traverse the String``    ``for` `i ``in` `range``(``1``, n):``      ` `    ``# If current row number is``    ``# not equal to previous row``    ``# then increment the moves``    ``if``(row[``ord``(s[i]) ``-``           ``ord``(``'a'``)] !``=` `row[``ord``(s[i ``-` `1``]) ``-``                            ``ord``(``'a'``)]):``      ``move ``+``=` `1``;` `    ``# Return the moves``    ``return` `move;` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``  ` `    ``# Given String str``    ``str` `=` `"geeksforgeeks"``;` `    ``# Function Call``    ``print``(numberMoves(``str``));` `# This code is contributed by Rajput-Ji Add`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG{` `// Function that calculates the moves``// required to print the current String``static` `int` `numberMoves(String s)``{``    ``// row1 has qwertyuiop, row2 has``    ``// asdfghjkl, and row3 has zxcvbnm``    ``// Store the row number of``    ``// each character``    ``int` `[]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 };` `    ``// String length``    ``int` `n = s.Length;` `    ``// Initialise move to 1``    ``int` `move = 1;` `    ``// Traverse the String``    ``for` `(``int` `i = 1; i < n; i++)``    ``{` `        ``// If current row number is``        ``// not equal to previous row``        ``// then increment the moves``        ``if` `(row[s[i] - ``'a'``] !=``            ``row[s[i - 1] - ``'a'``])``        ``{``            ``move++;``        ``}``    ``}` `    ``// Return the moves``    ``return` `move;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``// Given String str``    ``String str = ``"geeksforgeeks"``;` `    ``// Function Call``    ``Console.Write(numberMoves(str));``}``}` `// This code is contributed by sapnasingh4991`
Output:
```7

```

Time Complexity: O(N), where N is the length of the string.
Space Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up