-
Notifications
You must be signed in to change notification settings - Fork 0
/
String_rotated_by_2_places.cpp
66 lines (49 loc) · 1.15 KB
/
String_rotated_by_2_places.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to check if a string can be obtained by rotating
//another string by exactly 2 places.
bool isRotated(string str1, string str2)
{
int i = 0;
string str = str1;
str[0] = str1[str1.size()-2];
str[1] = str1[str1.size()-1];
while(str1[i+2] != NULL)
str[i+2] = str1[i], i++;
if(str == str2)
return true;
i = 0;
char temp = str1[0];
char temp1 = str1[1];
while(str1[i+2] != NULL)
{
str1[i] = str1[i+2];
i++;
}
str1[i] = temp;
str1[i+1] = temp1;
if(str1 == str2)
return true;
return false;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin>>t;
while(t--)
{
string s;
string b;
cin>>s>>b;
Solution obj;
cout<<obj.isRotated(s,b)<<endl;
}
return 0;
}
// } Driver Code Ends