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.


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.


  • 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:


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..

  • arpan agrawal

    Below is a bet­ter approach

    pub­lic class CheckingIfStringIsRotationOfOther {

    pub­lic sta­tic void main(String[] args) {

    String input = “sum­it­jain”, out­put = “tjainsumi”;

    if(function(input, out­put)){


    pub­lic sta­tic boolean function(String input, String out­put) {
    if(input.length() != output.length()) return false;

    int upper = 0 ;
    for(int i = 0 ; i < input.length();i++){
    if(input.substring(i).equals(output.substring(0,output.length() — i))){
    upper = i ; break;

    if(upper == 0) return true;
    if(input.substring(0,upper).equals(output.substring(output.length() — upper,output.length())))
    return true;

    return false;