Array ( [0] => [1] => questions [2] => Basic [3] => String-Rotation ) Codevita Previous Questions | String Rotation | THE INQUISITIVE





String Rotation

LEVEL:Beginner

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 Format

First line contains the string S
Next line contains the Number N
Next N lines contains a String and number K
Constraints
1 <= Length of original string <= 30
1<= q <= 10
string contains only alphabets and all characters will be in lowercase only
Input format

Output Format

Print Yes if exists else no


Example 1:

Input
theinquisitive
3
L 3
R 3
L 3
Output
Yes
Example 2:

Input
knowledge
4
L 2
R 5
R 2
L 3
Output
No
Example 3:

Input
boundless
3
L 10
R 5
L 5
Output
No

oops

Login to see Discussion




Approach

this problem should be divided into 3 parts
1)finding first element for every query
2)finding all the substrings of given string.
3) in those substrings checking the anagram of FIRSTCHARSTRING

For finding the first element, no need of rotating array simply keep counting the value that has given to be rotated
if the direction is L then add number of rotations to count varibale if the direction is R then subtract the number of rotations.
After finding all first elements in every query store in a string.

Now find all the substrings and at end of finding each substring check whether the substring is anagram to FIRSTCHARSTRING.
The optimal way for findings two strings are anagrams or not is initialize an array of size 26 and find the ascii value for each character in substring and increment by 1 and at same time find the ascii vlue for each character in FIRSTCHARSTRING now decrement by 1.
At final check whether all the elements in array of size 26 are 0 then those two strings are anagrams.
Retrun True
Print Yes.


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution