Friday, June 18, 2021
Home coding Write a program to check given string is rotation of each other.

# Write a program to check given string is rotation of each other.

Given two string s1 and s2 we have to find that whether the first string is rotation of another string.

Example:

``````input: s1:XXYZ  s2:XYZX
output:both string are rotation of each other

input: s1:xysds s2:psdwd
output:both string are not rotation of each other.``````

Logic:

``````Step 1: create a temporaray string .
step 2: assign the value ,temp_string= string1+string1;
step 3:here we will use find function of string to check whether string2 is present in temp string or not.
step 4:if it is present then it is rotation of eacg other.``````
```# include <bits/stdc++.h>
using namespace std;
int main()
{
string s1 = "MMNP", s2 = "MNPM";
if(s1.length()!=s2.length())
{
cout<<"string are not rotation of each other"<<endl;
}
else
{
string s3=s1+s1;
if(s3.find(s2)!=string::npos)//here we are checking in s3 ,is s2 present or not
{
cout<<"string are rotation of each other"<<endl;
}
else
{
cout<<"string are not rotation of each other"<<endl;
}
}

}
```

Output:

```string are rotation of each other
```

Time Complexity:O(N)

space complexity:O(N)

- Advertisment -
{"ft_lang_label":"__label__en","ft_lang_prob":0.6430617,"math_prob":0.9732663,"size":1010,"snap":"2021-21-2021-25","text_gpt3_token_len":284,"char_repetition_ratio":0.2166998,"word_repetition_ratio":0.12738854,"special_character_ratio":0.28613862,"punctuation_ratio":0.18181819,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9808011,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-18T15:47:43Z\",\"WARC-Record-ID\":\"<urn:uuid:329215a0-60d5-4ec6-88c1-1addeb9dbbed>\",\"Content-Length\":\"267374\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:937a059d-3bec-4242-9ca9-43e123f735c0>\",\"WARC-Concurrent-To\":\"<urn:uuid:820b1a42-c62e-47df-b4e1-022d55540ebd>\",\"WARC-IP-Address\":\"104.21.7.88\",\"WARC-Target-URI\":\"http://geeksprep.com/write-a-program-to-check-given-string-is-rotation-of-each-other/\",\"WARC-Payload-Digest\":\"sha1:OP5P3XGWUF5SCR2YI6VSLUVTP43AR2XG\",\"WARC-Block-Digest\":\"sha1:KXG57VP4IUG5SM7UCGGGSTRVDHNEI3NX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487637721.34_warc_CC-MAIN-20210618134943-20210618164943-00170.warc.gz\"}"}