📌  相关文章
📜  在基于QWERTY的键盘中键入单词所需的最小移动

📅  最后修改于: 2021-04-22 06:12:50             🧑  作者: Mango

给定一个仅包含小写英文字母的字符串S。任务是找到手指移动输入给字符串的最小次数。当您按下不在键盘上当前按下的键的行中的键时,将考虑该移动。

QWERTY键盘仅包含英文字母。

例子:

方法:可以通过首先在QWERTY键盘中设置每个字符的行号来完成。步骤如下:

  1. 将每个字符的行存储在array row []中
  2. 初始化移动= 1
  3. 遍历给定的字符串,并检查字符串中当前字符的行等于前一个字符或没有。
  4. 如果当前字符不相等,则增加移动量,因为我们需要在打印字符时更改当前行。
  5. 否则检查下一个字符对。

下面是上述方法的实现:

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


输出:
7








时间复杂度: O(N),其中N是字符串的长度。
空间复杂度: O(1)