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.
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 >> sInput: 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++ program for the above approach #include <bits/stdc++.h> 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 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 program for the above approach # Function that calculates the moves # required to print 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# 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 |
// Javascript program for the above approach // Function that calculates the moves // required to print the current string function numberMoves(s)
{ // row1 has qwertyuiop, row2 has
// asdfghjkl, and row3 has zxcvbnm
// Store the row number of
// each character
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 ];
// String length
var n = s.length;
// Initialise move to 1
var move = 1;
// Traverse the string
for ( var i = 1; i < n; i++) {
// If current row number is
// not equal to previous row
// then increment the moves
if (row[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]
!= row[s[i - 1].charCodeAt(0) - 'a' .charCodeAt(0)]) {
move++;
}
}
// Return the moves
return move;
} // Driver Code // Given String str
var str = "geeksforgeeks" ;
// Function Call
console.log(numberMoves(str));
// This code is contributed by Abhijeet Kumar(abhijeet19403) |
7
Time Complexity: O(N), where N is the length of the string.
Space Complexity: O(1)