Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIMinimum moves required to type a word in QWERTY based keyboard

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 =neveropen”
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 <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 = "neveropen";
 
    // 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 = "neveropen";
 
    // 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 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 = "neveropen";
 
    # 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 = "neveropen";
 
    // Function Call
    Console.Write(numberMoves(str));
}
}
 
// This code is contributed by sapnasingh4991


Javascript




// 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 = "neveropen";
 
    // Function Call
    console.log(numberMoves(str));
   
// This code is contributed by Abhijeet Kumar(abhijeet19403)


Output: 

7

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments