LeetCode #267 Palindrome Permutation II 迴文排列 II 267 Palindrome Permutation II 迴文排列 II

267 Palindrome Permutation II 迴文排列 II

Description:

Given a string s, return all the palindromic permutations (without duplicates) of it.

You may return the answer in any order. If s has no palindromic permutation, return an empty list.

Example:

Example 1:

Input: s = "aabb"
Output: ["abba","baab"]

Example 2:

Input: s = "abc"
Output: []

Constraints:

1 <= s.length <= 16
s consists of only lowercase English letters.

題目描述:

給定一個字符串 s ,返回 其重新排列組合後可能構成的所有迴文字符串,並去除重複的組合 。

你可以按 任意順序 返回答案。如果 s 不能形成任何迴文排列時,則返回一個空列表。

示例:

示例 1:

輸入: s = "aabb"
輸出: ["abba", "baab"]

示例 2:

輸入: s = "abc"
輸出: []

提示:

1 <= s.length <= 16
s 僅由小寫英文字母組成

思路:

回溯
先統計 s 的字符及數目
如果 s 中字符單個字符數超過 1 就返回空列表
只需要全排列 s 中的一半字符, 剩下的一半用倒序生成即可
時間複雜度爲 O(n!), 空間複雜度爲 O(n)

代碼:

C++:

class Solution 
{
public:
    vector<string> generatePalindromes(string s) 
    {
        unordered_map<char, int> count;
        vector<string> result;
        string even_s = "", odd_s = "";
        for (const auto& c : s) ++count[c];
        for (const auto& [c, v] : count) 
        {
            even_s += string(v >> 1, c);
            odd_s += string(v & 1, c);
        }
        if (odd_s.size() > 1) return result;
        string pre = even_s, rev_s;
        do 
        {
            rev_s = even_s;
            reverse(rev_s.begin(), rev_s.end());
            result.push_back(even_s + odd_s + rev_s);
            next_permutation(even_s.begin(), even_s.end());
        } 
        while (even_s != pre);
        return result;
    }
};

Java:

public class Solution {
    private Set<String> set = new HashSet<>();
    
    public List<String> generatePalindromes(String s) {
        int n = s.length(), k = 0, map[] = new int[128];
        char ch = 0, st[] = new char[n >> 1];
        if (!canPermutePalindrome(s, map)) return new ArrayList<>();
        for (int i = 0; i < 128; i++) if ((map[i] & 1) == 1) ch = (char)i;
        for (int i = 0; i < 128; i++) for (int j = 0; j < (map[i] >> 1); j++) st[k++] = (char)i;
        permute(st, 0, ch);
        return new ArrayList<String>(set);
    }
    
    private boolean canPermutePalindrome(String s, int[] map) {
        int count = 0, n = s.length();
        for (int i = 0; i < n; i++) {
            ++map[s.charAt(i)];
            if ((map[s.charAt(i)] & 1) == 0) --count;
            else ++count;
        }
        return count <= 1;
    }
    
    private void swap(char[] s, int i, int j) {
        char temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
    
    private void permute(char[] s, int l, char ch) {
        if (l == s.length) {
            set.add(new String(s) + (ch == 0 ? "" : ch) + new StringBuilder(new String(s)).reverse());
        } else {
            for (int i = l; i < s.length; i++) {
                if (s[l] != s[i] || l == i) {
                    swap(s, l, i);
                    permute(s, l + 1, ch);
                    swap(s, l, i);
                }
            }
        }
    }
}

Python:

class Solution:
    def generatePalindromes(self, s: str) -> List[str]:
        count, mid, pre_tail = Counter(s), '', ''
        for val, freq in count.items():
            pre_tail += val * (freq >> 1)
            mid += val * (freq & 1)
        return [''.join(half) + mid + ''.join(half)[::-1] for half in set(permutations(pre_tail))] if len(mid) <= 1 else []
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章