[LeetCode#267] Palindrome Permutation II

Problem:

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.

For example:

Given s = "aabb", return ["abba", "baab"].

Given s = "abc", return [].

Analysis:

This problem is very easy, it shares the same idea with "Strobogrammatic Number".
Use the rule you check a "Palindrome Permutation" to construct it!!! (two pointer!)

I have made one mistake in implementation.
Forget that when "len == 1", the string must a pilindrome string!

Wrong part:
if (len <= 1) {
    return ret;
}

Errors:
Input:
"a"
Output:
[]
Expected:
["a"]

Solution:

public class Solution {
    public List<String> generatePalindromes(String s) {
        if (s == null)
            throw new IllegalArgumentException("s is null");
        List<String> ret = new ArrayList<String> ();
        int len = s.length();
        if (len == 0) {
            return ret;
        }
        HashMap<Character, Integer> map = new HashMap<Character, Integer> ();
        for (char c : s.toCharArray()) {
            if (map.containsKey(c))
                map.put(c, map.get(c)+1);
            else
                map.put(c, 1);
        }
        int odd_count = 0;
        char odd_char = ‘a‘;
        for (char c : map.keySet()) {
            if (map.get(c) % 2 == 1) {
                odd_count++;
                odd_char = c;
            }
        }
        if (odd_count >= 2)
            return ret;
        if (odd_count == 1) {
            searchPath(map, odd_char + "", len, ret);
        } else{
            searchPath(map, "", len, ret);
        }
        return ret;
    }

    private void searchPath(HashMap<Character, Integer> map, String cur, int target, List<String> ret) {
        String new_cur = cur;
        int len = new_cur.length();
        if (len == target) {
            ret.add(new_cur);
            return;
        }
        for (char c : map.keySet()) {
            if (map.get(c) >= 2) {
                new_cur = c + cur + c;
                map.put(c, map.get(c) - 2);
                searchPath(map, new_cur, target, ret);
                map.put(c, map.get(c) + 2);
            }
        }
    }
}
时间: 2024-10-26 16:00:17

[LeetCode#267] Palindrome Permutation II的相关文章

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

267. Palindrome Permutation II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

【LeetCode】Palindrome Partitioning II

Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome p

[LeetCode]Palindrome Permutation II

递归做法 public class Solution { List<String> result = new ArrayList<String>(); public List<String> generatePalindromes(String s) { HashMap<Character, Integer> map = new HashMap<Character, Integer>(); for (int i = 0; i < s.len

Palindrome Permutation II 解答

Question Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s

[LeetCode#266] Palindrome Permutation

Problem: Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. General Analysis: This problem is easy. Basic idea is: iff s w

Java for LeetCode 132 Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"

【LeetCode】Palindrome Partitioning II 解题报告

[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&