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.
Input: S = “geeksforgeeks”
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”
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:
Time Complexity: O(N), where N is the length of the string.
Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether the string can be printed using same row of qwerty keypad
- Find the minimum number of preprocess moves required to make two strings equal
- Count minimum moves required to convert A to B
- Minimum Number of Platforms Required for a Railway/Bus Station | Set 2 (Map based approach)
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- C program to find and replace a word in a File by another given word
- Find the word from a given sentence having given word as prefix
- Modify the string such that every character gets replaced with the next character in the keyboard
- Sort numbers based on count of letters required to represent them in words
- Sort the strings based on the numbers of matchsticks required to represent them
- Count minimum number of "move-to-front" moves to sort an array
- Minimum number of moves to make all elements equal
- Minimum moves to reach from i to j in a cyclic string
- Minimum number of moves to make a binary array K periodic
- Find the minimum number of moves to reach end of the array
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Count minimum number of moves to front or end to sort an array
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimum Word Break
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.