Palindrome Permutation of a String in Java10 Sept 2024 | 8 min read A string inStr is provided to us. Our task is to find and print all the palindromes that is possible from the string inStr. Note that all of the characters of the string inStr have to be used to generate the palindromes. If a palindrome does not exist, then display an appropriate message. Given a string, we need to print all possible palindromes that can be generated using letters of that string. Examples: Example 1: Input String inStr = "ttpkkp" Output "ptkktp", "pkttkp", "tpkkpt", "tkppkt", "kpttpk", "ktpptk" are the palindromic permutation of the input string. Explanation: These are the only strings that have included all of the characters of the input string inStr and are also palindrome. Example 2: Input String inStr = "kkbbckdkd" Output "kkbdcdbkk" "kkdbcbdkk" "kbkdcdkbk" "kbdkckdbk" "kdkbcbkdk" "kdbkckbdk" "bkkdcdkkb" "bkdkckdkb" "bdkkckkdb" "dkkbcbkkd" "dkbkckbkd" "dbkkckkbd" Explanation: These are the only strings that have included all of the characters of the input string inStr and are also palindrome. Example 3: Input String inStr = "kkb" Output "kbk" Explanation: The only string "kbk" includes all of the characters of the input string inStr and is also a palindrome. Simple ApproachThe simple approach is to find out all the subsets of the string inStr. After that, filter out those subsets whose size is equal to the inStr. In the filtered-out subsets, check those subsets that are palindrome and print them. FileName: PalindromePermutation.java Output: For the string: ttpkkp The permutated palindrome strings are: kpttpk tpkkpt ktpptk ptkktp pkttkp tkppkt For the string: kkbbckdkd The permutated palindrome strings are: kbdkckdbk bdkkckkdb kkbdcdbkk kdkbcbkdk dkkbcbkkd kdbkckbdk bkkdcdkkb kbkdcdkbk dbkkckkbd kkdbcbdkk dkbkckbkd bkdkckdkb For the string: kkb The permutated palindrome string is: kbk Complexity Analysis: The program is doing the permutation of the string. The program is also using loops. However, the permutation of the string is the main time-consuming process, which makes the time complexity of the program O(n!). Also, the program is using hash set to store the permutation of the input string. Thus, the space complexity of the program is also O(n! x n), where n is the total number of characters present in the input string. After looking at the complexity analysis of the above program, it is obvious that some optimization is required. It is because the complexity of the above program is large. Before writing the program, let's see the following steps. Step 1: First, it is required to check whether using the characters of the input string can generate a palindrome or not. If it is not possible to generate a palindrome, then return. Step 2: After doing the checking in the first step, we can create the half part of the first palindrome string (lexicographically smallest) by considering the half frequency of each character of the input string. Step 3: Now iterate through all of the permutations that are possible of the half string, and every time adds the reverse of this part at the end. Step 4: Add the character having an odd frequency in between if the string is of the odd length for making the palindrome. Now, observe the following program. FileName: PalindromePermutation1.java Output: For the string: ttpkkp The permutated palindrome strings are: ktpptk ptkktp pkttkp tkppkt kpttpk tpkkpt For the string: kkbbckdkd The permutated palindrome strings are: dbkkckkbd kdbkckbdk kkbdcdbkk dkkbcbkkd kdkbcbkdk kbkdcdkbk bdkkckkdb bkkdcdkkb kbdkckdbk kkdbcbdkk bkdkckdkb dkbkckbkd For the string: kkb The permutated palindrome string is: kbk Complexity Analysis: The program is doing the permutation of half of the string. That makes the time complexity of the program O((n / 2)!). Also, the program is using hash set to store the permutation of the input string. Thus, the space complexity of the program is also O((n / 2)! x n / 2), where n is the total number of characters present in the input string. Next TopicGrepcode java.util.Date |
A virtual function or virtual method in an OOP language is a function or method used to override the behavior of the function in an inherited class with the same signature to achieve the polymorphism. When the programmers switch the technology from C++ to Java, they think...
4 min read
Java programming requires the use of exception management, and in the commercial world, where software must be highly dependable, maintainable, and scalable, adherence to best practices for exception handling becomes even more crucial. This article will go over some of the best Java exception-handling techniques that...
5 min read
In this section, we will discuss what is the neon numbers and also create a Java program to check if the given number is neon or not. Also, we will find all the neon numbers between a specified range. Neon Number A positive integer whose sum of digits...
3 min read
What is ? The stands for Java Micro Edition. It is a development and deployment platform of portable code for embedded and mobile devices (sensors, gateways, mobile phones, printers, TV set-top boxes). It is based on object-oriented Java. The has a robust user interface, great...
4 min read
Difference Between Hashmap and ConcurrentHashMap HashMap is a powerful data structure in Java used to store the key-pair values. It maps a value by its associated key. It allows us to store the null values and null keys. It is a non-synchronized class of Java collection. Whereas,...
4 min read
? Microservices architecture has gained immense popularity in recent years, offering a scalable and flexible approach to building and deploying applications. One of the critical aspects of a microservices-based system is how the individual services communicate with each other seamlessly. In this section, we will delve into...
2 min read
? Programming with Java is not dependent on any particular platform. It indicates that systems with Java interpreters can execute Java. It is the cause of Java's platform independence. The Java interpreter transforms Java bytecode (.class files) into code that the operating system can comprehend. We will...
3 min read
The concept of inheritance represents an essential principle among the four fundamental aspects of Object-Oriented Programming (OOP) in Java . A subclass can receive all fields and methods from its superclass through the inheritance mechanism. The feature enables developers to reuse code blocks and create maintainable and expandable...
3 min read
? Java classes that cannot be instantiated but can offer a set of methods and attributes for their concrete subclasses to implement are known as abstract classes. Abstract classes are frequently utilized to build a group of similar classes that share some behaviors but differ in other...
4 min read
In Java, the ">>>" operator is the right shift zero fill operator. When using the right shift operator in Java, the bits of a number are shifted to the right, and any bits that are shifted beyond the right end are discarded. The bits shifted from...
3 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