267. Palindrome permutation II

Source: Internet
Author: User
Tags repetition

    /** 267. Palindrome permutation II * 2016-3-13 by Mingyang * Given A string s, return all the palindromic permutations (wi Thout duplicates) of it.     Return an empty list if no palindromic permutation could is form.     * Given s = "Aabb", Return ["Abba", "Baab"].     * Given s = "abc", Return []. * This problem is relatively simple, and similar to permutation II, if you do not write the more important sentence, so long there will be a lot of repetition. The whole principle is to find all the combination possibilities, and then choose the appropriate * 1. Length standard: None (Fixed) * 2. Optional range: For all permutation of an array, the array is duplicated (so there is a sentence of judgment) and can be reversed before and after (so with visit ED).     Specific reference permutation II * 3. Take a step forward: SB joins this number, visited to True * 4. Step back: SB minus, visited to False * 5. Special case: to the length check. * 6. About repetition: Because there can be duplicate aabb there may be Abba and Abba, the first A in the first, the second A in the second*/      Public StaticList<string>Generatepalindromes (String s) {List<String> res=NewArraylist<string>(); Boolean[] visited=New Boolean[S.length ()]; Char[] model=S.tochararray (); StringBuffer SB=NewStringBuffer ();         DFS (RES,MODEL,VISITED,SB); returnRes; }      Public Static voidDFS (list<string> res,Char[] model,Boolean[] Visited,stringbuffer SB) {         if(Sb.tostring (). Length () = =model.length) {             if(Ispalindrome (sb.tostring ())) {Res.add (sb.tostring ()); }             return; }          for(inti=0;i<model.length;i++){             if(i > 0 &&!visited[i-1] && model[i] = = Model[i-1])//This is a very important!!!!!!!!!!.                  Continue; if(!Visited[i])                 {sb.append (model[i]); Visited[i]=true;                 DFS (RES,MODEL,VISITED,SB); Visited[i]=false; Sb.deletecharat (Sb.length ()-1); }         }     }

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.