forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_rotation.cpp
113 lines (106 loc) · 2.7 KB
/
string_rotation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
String Rotation
---------------
Problem Statement:
Rotate a given string in 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.
*/
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
// performing left shift operation
string shiftLeft(string s, int c)
{
return s.substr(c) + s.substr(0, c);
}
// performing right shift operation
string shiftRight(string s, int c)
{
return s.substr(c + 1) + s.substr(0, c + 1);
}
// DRIVER FUNCTION
int main()
{
char str[200], direction;
int queries, amount;
// first characters after performing each query is concatenated
string firstchar = "";
// getting the input string
cin >> str;
cin >> queries;
for (int query_num = 0; query_num < queries; query_num++)
{
// getting the direction and amount
cin >> direction >> amount;
// if direction is left, perform left shift
if (direction == 'L')
{
firstchar += shiftLeft(str, amount)[0];
}
// if direction is right, perform right shift
else if (direction == 'R')
{
firstchar += shiftRight(str, amount)[0];
}
}
map<char, int> mp1, mp2;
for (auto &i : firstchar)
// storing occurrence of characters in firstchar in map
mp1[i]++;
unsigned i = 0, d = firstchar.size();
// check if the size id greater than that of original string
if (d > (int)strlen(str))
{
cout << "NO";
return 0;
}
for (i = 0; i < d; i++)
// storing occurrence of characters in str in map
mp2[str[i]]++;
for (; i < (int)strlen(str); i++)
{
mp2[str[i]]++;
if (mp2[str[i - d]] == 1)
{
mp2.erase(mp2.find(str[i - d]));
}
else
mp2[str[i - d]]--;
// condition to check whether firstchar is an anagram of str
if (mp1 == mp2)
{
cout << "YES";
return 0;
}
}
// if firstchar is not an anagram of str, print NO
cout << "NO";
return 0;
}
/*
For ex:
Example1:
Input:
career
3
L 2
R 3
L 2
Output:
NO
Example 2:
Input:
carrace
2
L 3
R 3
YES
*/