-
Notifications
You must be signed in to change notification settings - Fork 22
/
check if string is rotated by two places
50 lines (39 loc) · 1.12 KB
/
check if string is rotated by two places
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
/*
Given two strings a and b. The task is to find if a string 'a' can be obtained by rotating another string 'b' by 2 places.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. In the next two line are two string
a and b.
Output:
For each test case in a new line print 1 if the string 'a' can be obtained by rotating string 'b' by two places else print 0.
Constraints:
1 <= T <= 50
1 <= length of a, b < 100
Example:
Input:
2
amazon
azonam
geeksforgeeks
geeksgeeksfor
Output:
1
0
Explanation:
Testcase 1: amazon can be rotated anti-clockwise by two places, which will make it as azonam.
Testcase 2: geeksgeeksfor can't be formed by any rotation from the given word geeksforgeeks.
*/
#include <bits/stdc++.h>
using namespace std;
bool isRotate(string a, string b){
if(a.size() != b.size()) return false;
if((a[0] == b[b.size()-2] && a[1] == b[b.size()-1]) ||
(b[0] == a[a.size()-2] && b[1]==a[a.size()-1])) return true;
else return false;
}
int main() {
int t; cin>>t; while(t--){
string a,b; cin>>a>>b;
cout<<isRotate(a,b)<<'\n';
}
return 0;
}