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.
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:
Please Login to comment...