Minimum moves required to type a word in QWERTY based keyboard
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:
- Store the row of each character in an array row[].
- Initialize move = 1.
- Traverse the given string and check if the row of the current character in the string is equal to the previous character or not.
- If current character is not equal then increment the move as we need to change the current row while printing the character.
- Else check for the next pairs of characters.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberMoves(string s)
{
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 };
int n = s.length();
int move = 1;
for ( int i = 1; i < n; i++) {
if (row[s[i] - 'a' ]
!= row[s[i - 1] - 'a' ]) {
move++;
}
}
return move;
}
int main()
{
string str = "geeksforgeeks" ;
cout << numberMoves(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int numberMoves(String s)
{
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 };
int n = s.length();
int move = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (row[s.charAt(i) - 'a' ] !=
row[s.charAt(i- 1 ) - 'a' ])
{
move++;
}
}
return move;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.print(numberMoves(str));
}
}
|
Python3
def numberMoves(s):
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 ];
n = len (s);
move = 1 ;
for i in range ( 1 , n):
if (row[ ord (s[i]) -
ord ( 'a' )] ! = row[ ord (s[i - 1 ]) -
ord ( 'a' )]):
move + = 1 ;
return move;
if __name__ = = '__main__' :
str = "geeksforgeeks" ;
print (numberMoves( str ));
|
C#
using System;
class GFG{
static int numberMoves(String s)
{
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 };
int n = s.Length;
int move = 1;
for ( int i = 1; i < n; i++)
{
if (row[s[i] - 'a' ] !=
row[s[i - 1] - 'a' ])
{
move++;
}
}
return move;
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
Console.Write(numberMoves(str));
}
}
|
Javascript
function numberMoves(s)
{
var 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 ];
var n = s.length;
var move = 1;
for ( var i = 1; i < n; i++) {
if (row[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]
!= row[s[i - 1].charCodeAt(0) - 'a' .charCodeAt(0)]) {
move++;
}
}
return move;
}
var str = "geeksforgeeks" ;
console.log(numberMoves(str));
|
Time Complexity: O(N), where N is the length of the string.
Space Complexity: O(1)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...