Java Program to Check if Two Strings are Cyclic Permutations of Each Other28 Mar 2025 | 4 min read It is a question that is usually asked in job interviews at prestigious IT companies such as Google, Microsoft, TCS, Accenture, etc. By figuring out the solution, one may assess the interviewee's logical reasoning, critical thinking, & problem-solving abilities. In this section we will create a Java program to check if two strings of the same length are cyclical permutations of one each other or not using different approaches. Cyclic PermutationA circular permutation occurs when a string can be changed into another by spinning its characters. Example:
Output:
There are various approaches to check if two strings are cyclic permutations of each other or not. In this section, we will discuss the approaches give below:
Using Sorting and Comparison ApproachSorting and Comparison is an approach where two strings are first converted into character arrays, then sorted and compared. If the sorted arrays match, the strings are considered permutations of each other. Let's implement the above approach in a Java program. File Name: PermutationChecker.java Output: No Time Complexity: O(n log n) Auxiliary Space: O(n) Using Counting and Comparison ApproachCounting and Comparison is an approach where character frequencies in both strings are counted using arrays. These frequency arrays are then compared to determine if the strings are permutations of each other. Let's implement the above approach in a Java program. File Name: PermutationChecker.java Output: Yes Time Complexity: O(n), where n is the length of the strings. Auxiliary Space: O(1) Using Single Array Counting ApproachThe approach uses a single array to track the frequency of characters in both strings. It ensures the strings are permutations by checking if their character counts match after processing. File Name: PermutationChecker.java Output: Yes Next TopicList vs Set in Java |
Java is a versatile programming language that allows developers to explore and implement various patterns and designs. One such intriguing pattern is the parallelogram pattern, which forms an aesthetically pleasing arrangement of characters. In this section, we will explore the concept of parallelogram patterns, learn how...
4 min read
A right-angled triangle containing successive natural integers is called the Floyd Triangle, after the computer scientist Robert Floyd. It is created by sequentially placing the numbers, beginning with 1 at the top, with each row carrying one more number than the one before it. The row and...
4 min read
The CountDownLatch class is another important class for concurrent execution. It is a synchronization aid that allows one or more than one thread to wait until a set of operations being performed in another thread is completed. It initializes with the count, which we pass to the...
5 min read
When it comes to text formatting and manipulating strings in Java, there are certain characters that play a crucial role. One such character is the line feed character. In Java, the line feed character is represented by the escape sequence "\n". It may seem like a...
4 min read
? The English meaning of truncate is to trim or prune, or cut something and the process of trimming is called truncation. In the computer science field, the term is often used in reference to data-types or variables (like String, floating-point numbers, etc.). It is a way...
5 min read
Every local variable and the final blank field will have an assigned value when any value is accessed. Access to the value will consist of the variable's name or an area that occurs in an expression, except in the left-hand operand of the assignment operator, "=". To...
15 min read
defines the connection between two classes that are set up through their objects. Association manages one-to-one, one-to-many, and many-to-many relationships. In Java, the multiplicity between objects is defined by the Association. It shows how objects communicate with each other and how they use the functionality...
9 min read
In this tutorial, we are going to learn about the Magnanimous number in Java. Magnanimous Number The magnanimous number are those numbers that contain at least 2 digits and always generates a prime number when the left part of the number is added with the right part of...
5 min read
An event is one of the most important concepts in Java. The change in the state of an object or behavior by performing actions is referred to as an Event in Java. Actions include button click, keypress, page scrolling, or cursor movement. Java provides a package java.awt.event...
7 min read
The ideas of covariance and contravariance come to light in the complex world of Java programming as crucial building blocks for producing durable, flexible, and adjustable software. These ideas, which have their roots in the field of polymorphism, are crucial in determining how types and techniques...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India