String Rotation

Check if one string is rotation of another.

JavaIntermediate
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first string: ");
        String s1 = sc.nextLine();
        System.out.print("Enter second string: ");
        String s2 = sc.nextLine();

        if (s1.length() == s2.length() && (s1 + s1).contains(s2)) {
            System.out.println("Rotation");
        } else {
            System.out.println("Not Rotation");
        }
        sc.close();
    }
}

Output

Enter first string: abcde
Enter second string: cdeab
Rotation

We double the first string and check if the second is a substring.