Topic:
Given A string s , return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could is form.
For example:
Given s = "aabb" , return ["abba", "baab"] .
Given s = "abc" , return [] .
Hint:
- If a palindromic permutation exists, we just need to generate the first half of the string.
- To generate all distinct permutations of a (half of) strings, use a similar approach from:permutations II or Next permutat Ion.
Links: http://leetcode.com/problems/palindrome-permutation-ii/
Exercises
Find all palindromic permutaitons that a string can generate. The subject was written very long. In general, we have to consider some of the following points:
- Whether a given s can generate palindrome
- Generated palindrome parity, is the odd word between the middle of the character is which one
- Using the principle of permutation II, the left half of the string is calculated
- Determine if you want to add an intermediate independent character to the parity of the palindrome
- Calculates the right half string, merges, joins to the result set, based on the calculated left half string
- Calculation of complexity (left for two brushes)
Time Complexity-o (2n), Space complexity-o (2n)
Public classSolution {Private BooleanIsoddpalindrome; PrivateString Singlechar; PublicList<string>Generatepalindromes (String s) {List<String> res =NewArraylist<>(); String evenpalindromestring=generateevenpalindromestring (s); if(evenpalindromestring.length () = = 0) { if( This. Isoddpalindrome) Res.add (Singlechar); returnRes; } Char[] arr =Evenpalindromestring.tochararray (); Arrays.sort (arr); StringBuilder SB=NewStringBuilder (); Boolean[] visited =New Boolean[Arr.length]; Generatepalindromes (res, SB, arr, visited); returnRes; } Private voidGeneratepalindromes (list<string> res, StringBuilder SB,Char[] arr,Boolean[] visited) { if(sb.length () = = Arr.length) {//we only get permutation of the result palindromeString s =sb.tostring (); Res.add ( This. isoddpalindrome? (S + This. Singlechar + reverse (s)): (S +reverse (s))); return; } for(inti = 0; i < arr.length; i++) { if(Visited[i] | | (i > 0 && arr[i] = = Arr[i-1] &&!visited[i-1])) {//Skip Duplicate Continue; } if(!Visited[i]) {Visited[i]=true; Sb.append (Arr[i]); Generatepalindromes (res, SB, arr, visited); Sb.setlength (Sb.length ()-1); Visited[i]=false; } } } PrivateString reverse (string s) {//Reverse String Char[] arr =S.tochararray (); intLo = 0, hi = s.length ()-1; while(Lo <hi) { Charc =Arr[lo]; Arr[lo++] =Arr[hi]; Arr[hi--] =C; } returnstring.valueof (arr); } PrivateString generateevenpalindromestring (string s) {//get even chars of palindromeSet<character> set =NewHashset<>(); StringBuilder SB=NewStringBuilder (); for(inti = 0; I < s.length (); i++) { Charc =S.charat (i); if(Set.contains (c)) {Set.remove (c); Sb.append (c); //Append only even counted chars}Else{Set.add (c); } } if(Set.size () <= 1) { if(set.size () = = 1) { for(CharChr:set) { This. Singlechar =string.valueof (CHR); } This. Isoddpalindrome =true; } returnsb.tostring (); } Else { return""; } }}
Reference:
Https://leetcode.com/discuss/53626/ac-java-solution-with-explanation
267. Palindrome permutation II