-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_8.cpp
45 lines (39 loc) · 951 Bytes
/
1_8.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
//
// 1_8.cpp
//
//
// Created by Pengyan Qin on 6/15/15.
//
//
#include <iostream>
#include <string>
using namespace std;
bool is_substring(string s1, string s2){ // check is s2 is a substring of s1
if(s1.length() == 0 || s2.length() == 0)
return false;
int start = 0; // start position in s1
int ind = 0; // compare position in s2
while((start + ind) < s1.length()){
if(s1[start + ind] == s2[ind]){
if(ind == s2.length() - 1)
return true;
else
ind++;
}
else{
++start;
ind = 0;
}
}
return false;
}
bool is_rotation(string s1, string s2){ // check if s2 is a rotation of s1
s2 = s2 + s2;
return is_substring(s2, s1); // check if s1 is a substring of s2
}
int main(){
string s1 = "waterbottle";
string s2 = "erbottlewate";
cout << is_rotation(s1, s2) << endl;
return 0;
}