Check if one string is Rotation of another string

Objective: Write an algorithm to check if one string is Rotation of another string. This question has been asked in the Amazon interview.


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

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

Input: Two Strings

Output: True or false based on whether strings are rotation of each other.


  • Make a new String by appending the appending the first string with itself
  • Check if second string is sub string of new String
Check if one string is Rotation of another string

Check if one string is Rotation of another string

Complete Code:


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

3 Responses

  1. Anshdeep singh says:

    can you tell me what do you actually mean by rotation of a string?

  2. Sai Yashodhar Vinay says:

    hi….here rotation of the is ‘sumitjain’ not ‘tjainsumi’… u mean left Rotate–> ‘tjainimus’ right Rotate–>’niajtsumi’…neither of these is not the answer ………..please give valid explanantion of rotating the string..

  3. arpan agrawal says:

    Below is a better approach

    public class CheckingIfStringIsRotationOfOther {

    public static void main(String[] args) {

    String input = “sumitjain”, output = “tjainsumi”;

    if(function(input, output)){


    public static boolean function(String input, String output) {
    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;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: