-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathis_shifted.cpp
More file actions
62 lines (46 loc) · 1.21 KB
/
is_shifted.cpp
File metadata and controls
62 lines (46 loc) · 1.21 KB
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
//
// Created by Mayank Parasar on 2020-02-15.
//
/*
You are given two strings, A and B. Return whether A can be shifted some number of times to get B.
Eg. A = abcde, B = cdeab should return true because A can be shifted 3 times to the right to get B. A = abc and B= acb should return false.
*/
#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool is_shifted(string stra, string strb) {
if(stra.length() != strb.length())
return false;
int i;
for(i = 0; i < strb.length(); i++) {
if(strb[i] == stra[0])
break;
}
// start from 'i' and reconstruct the word
// then match if it is same as stra.
int k = 0;
while(k != stra.length()) {
if(stra[k] == strb[i]) {
k++;
i++;
// check the loop over here...
if(i == strb.length())
i = 0;
}
else {
return false;
}
}
// if control comes here then it should be matched
return true;
}
int main() {
// string stra = "abcde";
// string strb = "cdeab";
string stra = "abc";
string strb = "acb";
cout << boolalpha;
cout << is_shifted(stra, strb);
return 0;
}