Cha Cha Slide C++

C++Server Side ProgrammingProgramming

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

 Live Demo

#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
raja
Updated on 02-Sep-2020 12:07:11

Advertisements