How to Check if One String is a Rotation of Another in Java? Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, to check if one string is a rotation of another, we can do string concatenation and substring matching. A rotated string is created by moving some characters from the beginning of the string to the end while maintaining the character order.Program to Check if One String is a Rotation of Another in JavaOne simple solution is to concatenate the first string with itself. If the second string is a substring of this concatenated string, then it is a valid rotation. This approach uses the contains() method in Java for substring checking.Example: Java // Java program to check if two given // strings are rotations of each other public class StringRotation { // Method to check if two strings // are rotations of each other public static boolean areRotations(String s1, String s2) { // Check if lengths are equal and s2 is a // substring of s1 concatenated with s1 return s1.length() == s2.length() && (s1 + s1).contains(s2); } public static void main(String[] args) { // Input strings String s1 = "ABCD"; String s2 = "CDAB"; // Check if s2 is a rotation of s1 if (areRotations(s1, s2)) { System.out.println("\"" + s2 + "\" is a rotation of \"" + s1 + "\"."); } else { System.out.println("\"" + s2 + "\" is not a rotation of \"" + s1 + "\"."); } // Additional test cases String s3 = "ABAD"; String s4 = "ADAB"; if (areRotations(s3, s4)) { System.out.println("\"" + s4 + "\" is a rotation of \"" + s3 + "\"."); } else { System.out.println("\"" + s4 + "\" is not a rotation of \"" + s3 + "\"."); } String s5 = "ABCD"; String s6 = "ACBD"; if (areRotations(s5, s6)) { System.out.println("\"" + s6 + "\" is a rotation of \"" + s5 + "\"."); } else { System.out.println("\"" + s6 + "\" is not a rotation of \"" + s5 + "\"."); } } } Output"CDAB" is a rotation of "ABCD". "ADAB" is a rotation of "ABAD". "ACBD" is not a rotation of "ABCD". Explanation: This example compares both string length and ensures the second string appears in the concatenated first string. Comment More infoAdvertise with us Next Article Check if a given string is Pangram in Java K kartik Follow Improve Article Tags : Misc Java Java Programs Java-Strings Java-String-Programs +1 More Practice Tags : JavaJava-StringsMisc Similar Reads Java Program To Check If A String Is Substring Of Another Write a java program for a given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples : Input: s1 = "for", s2 = "geeksforgeeks"Output: 5Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 3 min read Java Program to check if strings are rotations of each other or not Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = 2 min read Java Program to Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABA Output : True Input : GEEKS, EKSGE Output : True We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (lon 2 min read Check if a given string is Pangram in Java Given string str, the task is to write Java Program check whether the given string is a pangram or not. A string is a pangram string if it contains all the character of the alphabets ignoring the case of the alphabets. Examples: Input: str = "Abcdefghijklmnopqrstuvwxyz"Output: YesExplanation: The gi 3 min read Java Program To Check If A Linked List Of Strings Forms A Palindrome Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco 2 min read Java Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above ar 3 min read Like