[LeetCode] Palindrome Permutation I & II

Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome.

For example,
"code" -> False, "aab" -> True, "carerac" -> True.

Hint:

    1. Consider the palindromes of odd vs even length. What difference do you notice?
    2. Count the frequency of each character.
    3. If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times
 1 class Solution {
 2 public:
 3     bool canPermutePalindrome(string s) {
 4         vector<int> cnt(256, 0);
 5         for (auto a : s) ++cnt[a];
 6         bool flag = false;
 7         for (auto n : cnt) if (n & 1) {
 8             if (!flag) flag = true;
 9             else return false;
10         }
11         return true;
12     }
13 };

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 = "abc", return [].

Hint:

  1. If a palindromic permutation exists, we just need to generate the first half of the string.
  2. To generate all distinct permutations of a (half of) string, use a similar approach from: Permutations II or Next Permutation.

没按提示来,直接用的DFS,不知道符不符合要求。

 1 class Solution {
 2 public:
 3     void dfs(vector<string> &res, vector<int> &cnt, string &s, int l, int r) {
 4         if (l >= r) {
 5             res.push_back(s);
 6             return;
 7         }
 8         for (int i = 0; i < cnt.size(); ++i) if (cnt[i] >= 2) {
 9             cnt[i] -= 2;
10             s[l] = s[r] = i;
11             dfs(res, cnt, s, l + 1, r - 1);
12             cnt[i] += 2;
13         }
14     }
15     vector<string> generatePalindromes(string s) {
16         vector<int> cnt(256, 0);
17         for (auto a : s) ++cnt[a];
18         bool flag = false;
19         for (int i = 0; i < cnt.size(); ++i) if (cnt[i] & 1) {
20             if (!flag) {
21                 flag = true;
22                 s[s.length() / 2] = i;
23             } else {
24                 return {};
25             }
26         }
27         vector<string> res;
28         dfs(res, cnt, s, 0, s.length() - 1);
29         return res;
30     }
31 };
时间: 2024-08-05 08:33:57

[LeetCode] Palindrome Permutation I & II的相关文章

Palindrome Permutation I &amp; II

Palindrome Permutation I Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even

[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

Leetcode Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference d

[LeetCode] Palindrome Permutation 回文全排列

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference d

[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

[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

[LeetCode] Palindrome Partitioning II [12]

题目 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 &quot;Palindrome Partition II&quot; - Double DP

A so juicy and interesting problem! Recommend to everyone! A final solution may not jump into your mind immediately, of course, DP is not a trivial topic. So let's go step by step - it is more interesting that how you get to destination than destinat

LeetCode: Palindrome Partitioning II [132]

[题目] 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&