# Rotate String in Python

Suppose we have two strings, A and B. We will rotate the string A and check whether it matches with B at any position of rotating, if so then return true, otherwise false. For example, if A = 'abcde', and B = 'bcdea' So answer will be true, as A can be converted to B after rotating it.

To solve this, we will follow these steps −

• When both A and B are empty, then return true, when both are of different length then return false
• A := concatenate A after A
• i := 0, and j := 0
• while i < length of A
• if length of A – i + 1 < length of B, then return false
• while i < length of A and j < length of B and A[i] = B[j]
• increase i and j by 1
• if j = length of B then returns true
• if j is not 0, then decrease i by 1
• j := 0
• increase i by 1

## Example

Let us see the following implementation to get better understanding −

class Solution(object):
def rotateString(self, A, B):
"""
:type A: str
:type B: str
:rtype: bool
"""
if not A and not B:
return True
if len(A) != len(B):
return False
A = A*2
i = 0
j=0
#print(A,B)
while i < len(A):
if len(A)-i+1<len(B):
return False
while i<len(A) and j < len(B) and A[i] == B[j]:
#print("Here!",i,j)
i+=1
j+=1
if j == len(B):
return True
if j:
i-=1
j=0
i+=1

## Input

"abcde"
"cdeab"

## Output

true
{"ft_lang_label":"__label__en","ft_lang_prob":0.5322506,"math_prob":0.98738945,"size":797,"snap":"2019-51-2020-05","text_gpt3_token_len":243,"char_repetition_ratio":0.11727617,"word_repetition_ratio":0.0,"special_character_ratio":0.33249685,"punctuation_ratio":0.16161616,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99987054,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-29T14:27:53Z\",\"WARC-Record-ID\":\"<urn:uuid:0757567c-35a9-4d1a-b6ce-7c5a0908fde9>\",\"Content-Length\":\"22889\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5cb26f7f-d71d-4ace-8a6f-9d01dbd4862a>\",\"WARC-Concurrent-To\":\"<urn:uuid:4de5797a-fa26-4f6a-97ab-bce7daeda81b>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/rotate-string-in-python\",\"WARC-Payload-Digest\":\"sha1:HULFRBMKDCF64BV7UKQIEO5E3X4YLA6J\",\"WARC-Block-Digest\":\"sha1:DXDTBW4TUTOI2VGGQWMYK6HHMWKVZ4Q6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251799918.97_warc_CC-MAIN-20200129133601-20200129163601-00336.warc.gz\"}"}