Count the sequential key press
Count the sequence of key presses for characters in the given string by comparing it with the row-wise character (alphabet/digit) arrangements on a computer keyboard (QWERTY keyboard order), where keys can be pressed sequentially while typing the input string. Write a program to count the sequential key presses based on the given constraints
Constraints:
There are four rows in the keyboard layout. Sequential key presses can only be grouped together row-wise
If the same character is repeated in the given string, consider that also as a sequential key press.
If the identified sequence contains digits, consider the count as 2. Else it should be counted as 1. That is, if the given input is “we are 45177 people” then the count should be 7 (that is we-1, re-1, 45-2, 77-2 and op-1 results in count as 7)
Consider both left-to-right and right-to-left sequences for counting. For example, the string ‘Errera’ will result in a sequential count of 1. Here, Errer’ is considered as one sequence, like (Errer)a.
Spaces between words will break the key press sequence. For example, the string ‘Aaaaa aa 22’ will result in a sequential count of 4.
Input Format:
A string with or without space (Case insensitive)
Output Format:
Count of sequential key presses
Keyboard Layout:
Row 1: 1234567890
Row 2 qwertyuiop
Row 3: asdfghjkl
Row 4. zxcvbnm
Sample Input 1:
Princess Katy Perry underwent another operation in Zion hospital for typhoid
Sample Output 1:
12
Explanation:
Sequential combinations in the given string on comparing with the rows in keyboard layout Prince(ss) Ka(ty) P(err)y und(er)(we)nt anoth(er) (op)(er)at(io)n in Z(io)n hospital for (ty)ph(oi)d
Count:12
Sample Input 2:
ABCD Efhk
Sample output 2:
0
Sample input 3:
As IO oiuy poster
Sample output 3:
5
I am getting wrong output
Ashmita Shukla is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.