problem:
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 []
.
Analysis:
This problem was very easy, it shares the same idea with "strobogrammatic number""palindrome permutation" to const RUCT IT!!! (pointer! "len = = 1", the string must a Pilindrome string! Wrong part: if (Len <= 1) { return ret;} Errors:input:"A"output:[]expected:["a"]
Solution:
Public classSolution { PublicList<string>Generatepalindromes (String s) {if(s = =NULL) Throw NewIllegalArgumentException ("s is null"); List<String> ret =NewArraylist<string> (); intLen =s.length (); if(len = = 0) { returnret; } HashMap<character, integer> map =NewHashmap<character, integer> (); for(CharC:s.tochararray ()) { if(Map.containskey (c)) Map.put (c, Map.get (c)+1); ElseMap.put (c,1); } intOdd_count = 0; CharOdd_char = ' a '; for(CharC:map.keyset ()) { if(Map.get (c)% 2 = = 1) {Odd_count++; Odd_char=C; } } if(Odd_count >= 2) returnret; if(Odd_count = = 1) {SearchPath (map, Odd_char+ "", Len, ret); } Else{searchPath (map,"", Len, ret); } returnret; } Private voidSearchPath (Hashmap<character, integer> map, String cur,intTarget, list<string>ret) {String New_cur=cur; intLen =new_cur.length (); if(len = =target) {Ret.add (new_cur); return; } for(CharC:map.keyset ()) { if(Map.get (c) >= 2) {New_cur= c + cur +C; Map.put (c, Map.get (c)+ t); SearchPath (map, New_cur, Target, ret); Map.put (c, Map.get (c)-T); } } }}
[leetcode#267] Palindrome permutation II