267. Palindrome permutation II

Source: Internet
Author: User

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:

    1. If a palindromic permutation exists, we just need to generate the first half of the string.
    2. 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:

    1. Whether a given s can generate palindrome
    2. Generated palindrome parity, is the odd word between the middle of the character is which one
    3. Using the principle of permutation II, the left half of the string is calculated
    4. Determine if you want to add an intermediate independent character to the parity of the palindrome
    5. Calculates the right half string, merges, joins to the result set, based on the calculated left half string
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.