给定大小为26的字符串keyboardLayout表示键盘和字符串word的单行中存在的字符序列,任务是计算输入单词所需的总时间,从第0个键开始,如果移动到相邻的键需要单位时间。
例子:
Input: keyboardLayout = “abcdefghijklmnopqrstuvwxyz”, word = “dog”
Output: 22
Explanation:
Pressing the key ‘d’ requires 3 units of time ( i.e. ‘a’ -> ‘b’ -> ‘c’ -> ‘d’)
Pressing the key ‘o’ requires 11 units of time ( i.e. ‘d’ -> ‘e’ -> ‘f’ -> ‘g’ -> ‘h’ -> ‘i’ -> ‘j’ -> ‘k’ -> ‘l’ -> ‘m’ -> ‘n’ -> ‘o’)
Pressing the key ‘g’ requires 8 units of time ( i.e. ‘o’ -> ‘n’ -> ‘m’ -> ‘l’ -> ‘k’ -> ‘j’ -> ‘i’ -> ‘h’ -> ‘g’)
Therefore, the total time taken = 3 + 11 + 8 = 22.
Input: keyboardLayout = “abcdefghijklmnopqrstuvwxyz”, word = “abcdefghijklmnopqrstuvwxyz”
Output: 25
处理方法:按照以下步骤解决问题:
- 初始化一个向量,比如pos,来存储所有字符的位置。
- 初始化两个变量,比如last ,用于存储最后更新的索引,以及result,用于存储键入单词所花费的总时间。
- 迭代字符串word的字符:
- 初始化两个变量,比如目的地,以存储需要键入的下一个字符的索引,以及距离,以存储该索引与当前索引的距离。
- 将distance的值添加到result 。
- 将最后一个更新为destination 。
- 完成上述操作后,打印result的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate time
// taken to type the given word
int timeTakenToType(string& keyboardLayout,
string& word)
{
// Stores position of characters
vector pos(26);
// Iterate over the range [0, 26]
for (int i = 0; i < 26; ++i) {
// Set position of each character
char ch = keyboardLayout[i];
pos[ch - 'a'] = i;
}
// Store the last index
int last = 0;
// Stores the total time taken
int result = 0;
// Iterate over the characters of word
for (int i = 0; i < (int)word.size(); ++i) {
char ch = word[i];
// Stores index of the next character
int destination = pos[ch - 'a'];
// Stores the distance of current
// character from the next character
int distance = abs(destination - last);
// Update the result
result += distance;
// Update last position
last = destination;
}
// Print the result
cout << result;
}
// Driver Code
int main()
{
// Given keyboard layout
string keyboardLayout
= "acdbefghijlkmnopqrtsuwvxyz";
// Given word
string word = "dog";
// Function call to find the minimum
// time required to type the word
timeTakenToType(keyboardLayout, word);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to calculate time
// taken to type the given word
static void timeTakenToType(String keyboardLayout,
String word)
{
// Stores position of characters
int[] pos = new int[26];
// Iterate over the range [0, 26]
for (int i = 0; i < 26; i++) {
// Set position of each character
char ch = keyboardLayout.charAt(i);
pos[ch - 'a'] = i;
}
// Store the last index
int last = 0;
// Stores the total time taken
int result = 0;
// Iterate over the characters of word
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
// Stores index of the next character
int destination = pos[ch - 'a'];
// Stores the distance of current
// character from the next character
int distance = Math.abs(destination - last);
// Update the result
result += distance;
// Update last position
last = destination;
}
System.out.println(result);
}
// Driver Code
public static void main(String[] args)
{
// Given keyboard layout
String keyboardLayout
= "acdbefghijlkmnopqrtsuwvxyz";
// Given word
String word = "dog";
// Function call to find the minimum
// time required to type the word
timeTakenToType(keyboardLayout, word);
}
}
// This code is contributed by aadityapburujwale.
Python3
# Python3 program for the above approach
# Function to calculate time
# taken to type the given word
def timeTakenToType(keyboardLayout, word):
# Stores position of characters
pos = [0]*(26)
# Iterate over the range [0, 26]
for i in range(26):
# Set position of each character
ch = keyboardLayout[i]
pos[ord(ch) - ord('a')] = i
# Store the last index
last = 0
# Stores the total time taken
result = 0
# Iterate over the characters of word
for i in range(len(word)):
ch = word[i]
# Stores index of the next character
destination = pos[ord(ch) - ord('a')]
# Stores the distance of current
# character from the next character
distance = abs(destination - last)
# Update the result
result += distance
# Update last position
last = destination
# Prthe result
print (result)
# Driver Code
if __name__ == '__main__':
# Given keyboard layout
keyboardLayout = "acdbefghijlkmnopqrtsuwvxyz"
# Given word
word = "dog"
# Function call to find the minimum
# time required to type the word
timeTakenToType(keyboardLayout, word)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
public class GFG {
// Function to calculate time
// taken to type the given word
static void timeTakenToType(String keyboardLayout,
String word)
{
// Stores position of characters
int[] pos = new int[26];
// Iterate over the range [0, 26]
for (int i = 0; i < 26; i++) {
// Set position of each character
char ch = keyboardLayout[i];
pos[ch - 'a'] = i;
}
// Store the last index
int last = 0;
// Stores the total time taken
int result = 0;
// Iterate over the characters of word
for (int i = 0; i < word.Length; i++) {
char ch = word[i];
// Stores index of the next character
int destination = pos[ch - 'a'];
// Stores the distance of current
// character from the next character
int distance = Math.Abs(destination - last);
// Update the result
result += distance;
// Update last position
last = destination;
}
Console.WriteLine(result);
}
// Driver Code
public static void Main(String[] args)
{
// Given keyboard layout
String keyboardLayout
= "acdbefghijlkmnopqrtsuwvxyz";
// Given word
String word = "dog";
// Function call to find the minimum
// time required to type the word
timeTakenToType(keyboardLayout, word);
}
}
// This code is contributed by shikhasingrajput
Javascript
22
时间复杂度: O(N),其中 N 是字符串单词的大小。
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。