You are given a string ‘S’ of length ‘N’. The string can be encoded using the following rules:
1) If the ‘i-th’ character is a vowel, change it to the next character in the alphabetical sequence. For example, the next character of ‘o’ is ‘p’.
2) If the ‘i-th’ character is a consonant, change it to the previous character in the alphabetical sequence. For example, the previous character of ‘h’ is ‘g’.
3) The next character of ‘z’ is ‘a’.
4) The previous character of ‘a’ is ‘z’.
Find the encoded string.
Example :‘N’ = 4, ‘S’ = “code”
Character ‘c’ gets changed to ‘b’.
Character ‘o’ gets changed to ‘p’.
Character ‘d’ gets changed to ‘c’.
Character ‘e’ gets changed to ‘f’.
Encoded string = “bpcf”
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains an integer ‘N’, denoting the length of the string.
The second line of each test case contains a string ‘S’ of length ‘N’.
Output Format :
Print a string that denotes the encoded string of the original string.
Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
The Sum of ‘N’ over all test cases is <= 10^5.
The string ‘S’ contains only lowercase letters.
Time Limit: 1 sec
2
3
dog
4
cazz
cpf
bbyy
For the first test case :
Character ‘d’ gets changed to ‘c’.
Character ‘o’ gets changed to ‘p’.
Character ‘g’ gets changed to ‘f’.
Encoded string = “cpf”.
For the second test case :
Character ‘c’ gets changed to ‘b’.
Character ‘a’ gets changed to ‘b’.
Character ‘z’ gets changed to ‘y’.
2
4
gjmf
3
abc
file
bab
Simulate the problem statement.
Approach :
Algorithm :
O(N), where ‘N’ is the length of the given string.
Since we are traversing the string once and the time to check if a character is a vowel is constant. So the overall Time Complexity is O(N).
O(1)
We only need constant extra space here. So the total Space Complexity is constant, i.e. O(1).