Last Updated: 1 Nov, 2020

Left And Right Rotation Of A String

Easy
Asked in companies
AmazonAmerican ExpressBajaj Electricals Ltd

Problem statement

You are given a string 'str' and an integer 'D'. Your task is to rotate the given string left (anticlockwise) and right (clockwise) by 'D' units from the starting index. You are required to return the rotated string.

Example :

Left-Right Rotation

Input Format :
The first line of input contains an integer 'T' which denotes the number of test cases or queries to be run.

Then the test cases follow:

The first line of each test case contains the string 'str'.

The second and last line of each test case contains an integer 'D', representing the number of units by which the string is to be rotated left or right. 
Output format :
For each test case, print the left and right rotations of the string separated by a single space.

Output for every test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.

1. leftRotate(): This function should return the string after left rotation.

2. rightRotate(): This function should return the string after right rotation.
Constraints :
1 <= T <= 10
1 <= |str| <= 10^5
1 <= D <= 10^5
Where |str| denotes the length of the string str.

Time Limit: 1 sec

Approaches

01 Approach

The idea is to use an additional string to store the copies of required substrings. So we will Make initialize an empty string named ‘ANS’. 

 

  • For left rotation of given string, append last ‘N’ - ‘D’ characters to ‘ANS’, then append the remaining first ‘D’ characters to the ‘ANS’.
  • The required string after left rotation is ‘ANS’.
  • Return the current state of ‘ANS’.
  • For right rotation again initialize ans with an empty string (i.e. ‘ANS’ = “”) and append last ‘D’ characters of the given string to the ‘ANS’, then append the remaining ‘N’ - ‘D’ characters of the given string from beginning to the ans. Now the required string after right rotation is ‘ANS’.
  • Print the current state of ‘ANS’.

 

Explanation with an example: 

 

Let the given string = ”abcdef” , D = 2.

Initialize ‘ANS’ = ””, for left rotation: 

  • ‘ANS’ = “” + “cdef”. Now ‘ANS’ become equal to “cdef”
  • ‘ANS’ =  “cdef” + “ab”. Now ‘ANS’ become equal “cdefab” which is the expected result.

For right rotation again initialize ans to empty:

  • ‘ANS’ = “” + “ef”. Now ‘ANS’  become equal to “ef”.
  • ‘ANS’ = “ef” + “abcd”. Now ‘ANS’ become equal to “efabcd” which is the expected result.

02 Approach

  • In case of left rotation of the string, reverse the substring of length D starting from the beginning. Then reverse the substring of length N-d start from the D-1th index. Now at the last rotate the whole string, the result we get is the required string after left rotation.
  • In the case of right rotation, it's the same as the left rotation of the string with N-D units from the beginning.

 

Explanation with an example: 

 

Let the given string is ”abcdef” and D = 2.

  • In Left Rotation : “bacdef” -> “bafedc” -> “cdefab”.
  • In Right Rotation : LeftRotate(string,4) -> “dcbaef” -> “dcbafe” -> “efabcd”