资讯专栏INFORMATION COLUMN

[LeetCode] 267. Palindrome Permutation II

huashiou / 1144人阅读

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.

Example

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

Solution

</>复制代码

  1. class Solution {
  2. public List generatePalindromes(String s) {
  3. List res = new ArrayList<>();
  4. if (s == null || s.length() == 0) return res;
  5. //create a map to record counts of chars: int[256]
  6. //count the chars that have odd number of count: <= 1 means valid palindrome
  7. int[] map = new int[256];
  8. int odd = 0;
  9. for (char ch: s.toCharArray()) {
  10. map[ch]++;
  11. if ((map[ch]&1) == 1) odd++;
  12. else odd--;
  13. }
  14. if (odd > 1) return res;
  15. //get mid string: should have length of 1 or 0
  16. String mid = "";
  17. int halfLen = 0;
  18. for (int i = 0; i < 256; i++) {
  19. if (map[i] > 0) {
  20. //find the odd counted char, add to mid
  21. if ((map[i]&1) == 1) {
  22. mid += (char)i;
  23. map[i]--;
  24. }
  25. //reduce count to half for each char
  26. map[i] /= 2;
  27. //update half palindrome length
  28. halfLen += map[i];
  29. }
  30. }
  31. dfs(map, "", mid, halfLen, res);
  32. return res;
  33. }
  34. private void dfs(int[] map, String temp, String mid, int len, List res) {
  35. if (temp.length() == len) {
  36. StringBuilder sb = new StringBuilder(temp).reverse();
  37. res.add(temp+mid+sb.toString());
  38. return;
  39. }
  40. for (int i = 0; i < 256; i++) {
  41. if (map[i] > 0) {
  42. map[i]--;
  43. dfs(map, temp+(char)i, mid, len, res);
  44. map[i]++;
  45. }
  46. }
  47. }
  48. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71518.html

相关文章

  • LC 267 Palindrome Permutation II

    摘要:判断一个能否组成一个第一次出现增加一,第二次出现减少一。出现偶数次的最终对被抵消。出现基数词的则会让加一。类似于,奇数个的那个单独考虑,必须放中间。得到各个字符一半数量的长度数字的终止条件,利用的对称性输出结果。 Given a string s, return all the palindromic permutations (without duplicates) of it. R...

    lowett 评论0 收藏0
  • [Leetcode] Palindrome Permutation 回文变换

    摘要:最笨的方法就是用的解法,找出所有的,然后再用中判断回文的方法来判断结果中是否有回文。而中心对称点如果是字符,该字符会是奇数次,如果在两个字符之间,则所有字符都是出现偶数次。 Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. F...

    svtter 评论0 收藏0
  • [LeetCode] 266. Palindrome Permutation

    Problem Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: codeOutput: falseExample 2: Input: aabOutput: trueExample 3: Input: careracOutput: true Solu...

    jlanglang 评论0 收藏0
  • leetcode 部分解答索引(持续更新~)

    摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...

    leo108 评论0 收藏0
  • leetcode132. Palindrome Partitioning II

    摘要:题目要求现在有一个字符串,将分割为多个子字符串从而保证每个子字符串都是回数。我们只需要找到所有可以构成回数的并且得出最小值即可。即将字符作为,将字符所在的下标列表作为。再采用上面所说的方法,利用中间结果得出最小分割次数。 题目要求 Given a string s, partition s such that every substring of the partition is a ...

    jeyhan 评论0 收藏0

发表评论

0条评论

huashiou

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<