You are given a string, 'MESSAGE'. The receiver of this ‘MESSAGE’ has a set of instructions on how to decrypt the message. In order to decrypt the message, we need to perform some rotation operations on the string.
These rotations can either be left rotations or right rotations. The set of instructions contains information about which type of rotation is to be performed and how many times. Your task is to determine the decrypted message.
Note:
Performing a 'left rotation' means deleting the first character of the string and appending it to the end of the string.
Performing a 'right rotation' means deleting the last character of the string and appending it to the beginning of the string.
For example, if we perform a left rotation on the string “coding”, it will become “odingc”. If we perform a right rotation on the string “ninja”, it will become “aninj”.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains a single string, 'MESSAGE',
The second line of each test case contains an integer, ‘N’, denoting the number of rotations we need to perform in order to decrypt the message. Then, ‘N’ lines follow.
Each line contains two space-separated integers, ‘A’ and ‘B’. If ‘A’ is ‘-1’, you need to perform the left rotation on the string ‘MESSAGE’, ‘B’ times. If ‘A’ is ‘1’, you need to perform the right rotation, ‘B’ times.
Output Format:
For each test case, return a single string, denoting the decrypted message after performing all the required operations.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |MESSAGE| <= 500
1 <= N <= 1000
N is always even
Time Limit: 1sec
1
abcde
4
-1 2
1 1
-1 2
-1 1
eabcd
Initially, the given string is “abcde”. After performing the left rotation on it two times, we will get “cdeab”. After performing the right rotation on this string once, we will get “bcdea”. Next time, the string will become “deabc”. And finally, the string will become “eabcd”. Hence, we print the final result.
1
babb
3
-1 1
1 2
1 3
babb
Initially, the given string is “babb”. After performing the left rotation on it once, we will get “abbb”. After performing the right rotation on this string two times, we will get “bbab”. And finally, the string will become “babb”. Hence, we print the final result.
If the size of the string is ‘LEN’, rotation of ‘LEN’ effectively means no rotation.
The approach is to observe the fact that if we perform a rotation operation, ’LEN * K’ times, where ‘LEN’ is the length of the string and ‘K’ is an arbitrary number, then we are effectively making no rotation in either direction. So, if we need to perform a rotation operation, ‘X’ number of times, we can perform it ‘X % LEN’ times, and we will still get the same result.
Another observation here is that if we want to perform the left rotation operation, ‘X’ times, then we can simply delete the first ‘X’ characters from the string and append them at the last of the string. Similarly, for the right rotation operation, we can delete the last ‘X’ characters and append them at the beginning of the string.
O(N * M), where ‘N’ is the total number of rotation operations to be performed and ‘M’ is the length of the given string.
There are a total of ‘N’ rotation operations to be performed. In each operation, we need to make changes to the string which, in the worst case, can take O(M) time. So, the overall complexity will become O(N * M).
O(1).
We are not using any extra auxiliary space.