Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Check if one string is Rotation of another string

Objec­tive: Write an algo­rithm to check if one string is Rota­tion of another string. This ques­tion has been asked in the Ama­zon interview.

Exam­ple:

Input Strings : 'sumitjain' and 'tjainsumi'
Output : true

Input String : 'Jaain' and 'ainJ'
Output: false

Input: Two Strings

Out­put: True or false based on whether strings are rota­tion of each other.

Approach:

  • Make a new String by append­ing the append­ing the first string with itself
  • Check if sec­ond string is sub string of new String
Check if one string is Rotation of another string

Check if one string is Rota­tion of another string

Com­plete Code:


Out­put:

Is 'sumitjain' and 'tjainsumi' are rotated?? : true

You may also like...

  • Anshdeep singh

    can you tell me what do you actu­ally mean by rota­tion of a string?

  • Sai Yashod­har Vinay

    hi.…here rota­tion of the is ‘sum­it­jain’ not ‘tjain­sumi’… u mean left Rotate–> ‘tjain­imus’ right Rotate–>‘niajtsumi’…neither of these is not the answer .….……please give valid explanan­tion of rotat­ing the string..