Left and Right Rotation of a String

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
CodingNinjas
author
2y

I first separate string to words and stored them in array. Then passed every element of array to a function which returns the rotated word. In function I used reverse technique to rotate array and thi...read more

CodingNinjas
author
2y
Brute Force

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 ‘...read more
CodingNinjas
author
2y
In-Place Rotation
  • 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 ...read more
Add answer anonymously...
Nagarro Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter