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 |
Java programming supports different data structures like array, linked list, stack, queue, etc. Each data structure has operations such as insertion, deletion, searching an element. And to implement these operations Java programming provides built in classes and methods. In this section, we will understand the pop...
4 min read
In this section, we will learn what is a repdigit numbers and also create Java programs to check if the given number is a repdigit number or not. The repdigit number program is frequently asked in Java coding interviews and academics. Repdigit Number Repdigit is short for repeated...
2 min read
The problem of the closest number is one of the most popular programming interview questions since the problem's main task is to look for a number in an array that has the closest distance to a particular given number. This problem is implemented in many computational...
4 min read
Java program to calculate area and circumference of circle In this section, we will create a Java program to calculate the area and circumference of the circle. Area of Circle Formulas When the radius is known: When the diameter is known: When the circumference is known: Where, A: is an area of a...
3 min read
How to convert byte array to String in Java? The process of converting a byte array to a String is called decoding. This process requires a Charset. Though, we should use charset for decoding a byte array. There are two ways to convert byte array to String: By using...
7 min read
Bucket sort is a sorting technique in which elements are first uniformly divided into several groups called buckets. After that, elements are sorted by any sorting algorithm, and finally, it gathered the elements in a sorted manner. In this section, we will learn how bucket sort...
5 min read
A key idea in object-oriented programming is polymorphism, which enables objects of various kinds to be considered instances of a single superclass or interface. Java has two methods for achieving polymorphism: static polymorphism (sometimes called compile-time polymorphism) and dynamic polymorphism (often called runtime polymorphism). The...
4 min read
? Java is a potent programming language that can be used to create a vast array of desktop, online, and mobile apps. The List interface is one of Java's core data structures. A list is a group of elements that are arranged in a certain order and...
4 min read
In this section, we will learn what is Pig Latin word and how to translate or encode a word into a Pig Latin word. Also, we will implement the logic in a JavaM program to find the Pig Latin string. What is Pig Latin? Pig Latin is a...
3 min read
The java.text.ChoiceFormat is a class containing an equals() as a function. When two ChoiceFormat objects are compared, the ChoiceFormat class is utilized to determine the Boolean value of the comparison. Syntax: public boolean equals(Object obj_name) Parameter: -where Obj is a parameter, an entirely distinct ChoiceFormat object for comparison, that...
2 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