0% found this document useful (0 votes)
327 views

String Rotation

Rotate a given string based on direction (left/right) and magnitude provided. Track the first character of the string after each rotation to form a new string. Check if this new string is an anagram of any substring of the original string. If yes, output is "YES", otherwise "NO".

Uploaded by

keerthana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
327 views

String Rotation

Rotate a given string based on direction (left/right) and magnitude provided. Track the first character of the string after each rotation to form a new string. Check if this new string is an anagram of any substring of the original string. If yes, output is "YES", otherwise "NO".

Uploaded by

keerthana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

String Rotation Question

Problem Description

Rotate a given String in the specified direction by specified magnitude.

After each rotation make a note of the first character of the rotated String, After all rotation are performed the accumulated
first character as noted previously will form another string, say FIRSTCHARSTRING.

Check If FIRSTCHARSTRING is an Anagram of any substring of the Original string.


If yes print "YES" otherwise "NO". 

Input
The first line contains the original string s. The second line contains a single integer q. The ith of the next q lines contains
character d[i] denoting direction and integer r[i] denoting the magnitude.

Constraints
1 <= Length of original string <= 30
1<= q <= 10
Sample Test Case 1:
String Rotation Question
Input:
carrace
3
L2
R2
L3
Output:
No

Explanation
After applying all the rotations the FIRSTCHARSTRING string will be "rcr" which is not anagram of any sub string of
original string "carrace".
Step 1: Understand the question
Sample Test Case 1:
Input:
carrace
3 C A R R A C E
L2
R2
L3
Output:
No
Step 1: Understand the question
Sample Test Case 1:
Input:
carrace
3 R R A C E C
L2
R2
L3
Output:
No
A

FirstCharArray R
Step 1: Understand the question
Sample Test Case 1:
Input:
carrace
3 R
C R
A A C E C A
L2
R2
L3
Output:
No

FirstCharArray R C
Step 1: Understand the question
Sample Test Case 1:
Input:
carrace
3 RC AA CR ER AC AC E
R
L2
R2
L3
Output:
No

FirstCharArray R C R
Step 2: Figure Out the Logic
Sample Test Case 1: Convert to char array C, A, R, R, A, C, E
Input:
CARRACE Rotate the strings – Left or Right According to the given input
3
L2 Store the first characters of the rotated string - firstcharstring
R2
L3 Check if the characters in the firstcharstring is an anagram of subset of
Output: the character array using substr method
No
Anagrams:

Listen & Silent


Step 3: Write Pseudo Code

Step 1: Get the input as string

Step 2: Split and save it as a character array

Step 3: Rotate the character array – left or right to the number of positions given according to the input

Step 4: Note the first characters of the array

Step 5: check if the first character array is an anagram of the subset of the given string

Step 6: If yes, print Yes else print No

You might also like