Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Cha Cha Slide C++
Suppose we have two strings s and t. we have to check whether s is rotation of t or not, in other words, can we get t after rotating s?
So, if the input is like s = "helloworld" and t = "worldhello", then the output will be True.
To solve this, we will follow these steps −
-
if size of s0 is not equal to size of s1, then −
return false
s := s0 concatenate s0
return true when s1 is in s, otherwise 0
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool solve(string s0, string s1) {
if(s0.size() != s1.size())
return false;
string s = s0 + s0;
return s.find(s1) != string::npos;
}
};
int main(){
Solution ob;
cout << (ob.solve("helloworld", "worldhello"));
}
Input
"helloworld", "worldhello"
Output
1
Advertisements
