Before writing the program, let's see the following steps. It is because the complexity of the above program is large. 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.Īfter looking at the complexity analysis of the above program, it is obvious that some optimization is required. Also, the program is using hash set to store the permutation of the input string. However, the permutation of the string is the main time-consuming process, which makes the time complexity of the program O(n!). Kbdkckdbk bdkkckkdb kkbdcdbkk kdkbcbkdk dkkbcbkkd kdbkckbdk bkkdcdkkb kbkdcdkbk dbkkckkbd kkdbcbdkk dkbkckbkd bkdkckdkbĬomplexity Analysis: The program is doing the permutation of the string. Kpttpk tpkkpt ktpptk ptkktp pkttkp tkppkt ("The permutated palindrome string is: ") ("The permutated palindrome strings are: ") PalindromePermutation obj = new PalindromePermutation() ĪrrayList list = obj.palindromeStrings(inStr) creating an object of the class PalindromePermutation then it means the string temp is a palindrome iterator for iterating over the hash set list that stores the permuted palindrome string Private void permuteString(String str1, int lt, int rt)įor (int j = lt j palindromeStrings(String str) a set that contains all the permutations importing HashSet, ArrayList, Iterator In the filtered-out subsets, check those subsets that are palindrome and print them. After that, filter out those subsets whose size is equal to the inStr. The simple approach is to find out all the subsets of the string inStr. The only string "kbk" includes all of the characters of the input string inStr and is also a palindrome. "kkbdcdbkk" "kkdbcbdkk" "kbkdcdkbk" "kbdkckdbk" "kdkbcbkdk" "kdbkckbdk" "bkkdcdkkb" "bkdkckdkb" "bdkkckkdb" "dkkbcbkkd" "dkbkckbkd" "dbkkckkbd" These are the only strings that have included all of the characters of the input string inStr and are also palindrome. "ptkktp", "pkttkp", "tpkkpt", "tkppkt", "kpttpk", "ktpptk" are the palindromic permutation of the input string. Given a string, we need to print all possible palindromes that can be generated using letters of that string. If a palindrome does not exist, then display an appropriate message. Note that all of the characters of the string inStr have to be used to generate the palindromes. Our task is to find and print all the palindromes that is possible from the string inStr. Next → ← prev Palindrome Permutation of a String in JavaĪ string inStr is provided to us.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |