/** 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