• 266. 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?

    链接: http://leetcode.com/problems/palindrome-permutation/

    题解:

    判断一个String是否可以组成一个Palindrome。我们只需要计算单个字符的个数就可以了,0个或者1个都是可以的,超过1个则必不能成为Palindrome。双数的字符我们可以用Set来even out。

    Time Complexity - O(n), Space Complexity - O(n)

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            if(s == null) {
                return false;
            }
            Set<Character> set = new HashSet<>();
            for(int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if(set.contains(c)) {
                    set.remove(c);
                } else {
                    set.add(c);
                }
            }
            
            return set.size() <= 1;
        }
    }

    二刷:

    Java:

    Time Complexity - O(n), Space Complexity - O(n)

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            if (s == null) {
                return false;
            }
            Set<Character> set = new HashSet<>();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (!set.add(c)) {
                    set.remove(c);
                }
            }
            return set.size() <= 1;
        }
    }

    使用Bitmap:

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            if (s == null || s.length() == 0) {
                return false;
            }
            int[] bitArr = new int[256];
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                bitArr[c]++;
            }
            boolean foundSingleChar = false;
            for (int i = 0; i < 256; i++) {
                if (bitArr[i] % 2 != 0) {
                    if (foundSingleChar) {
                        return false;
                    } else {
                        foundSingleChar = true;
                    }
                }
            }
            return true;
        }
    }

    简写后的bitmap,因为没有set的remove(),所以速度更快一些,当然这是我们假定字符都属于ascii的前提下。

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            if (s == null || s.length() == 0) {
                return false;
            }
            int[] bitArr = new int[256];
            int count = 0;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                bitArr[c]++;
                count = bitArr[c] % 2 != 0 ? count + 1 : count - 1;
            }
            return count <= 1;
        }
    }

    Python: 

    来自StefanPochmann

    class Solution(object):
        def canPermutePalindrome(self, s):
            """
            :type s: str
            :rtype: bool
            """
            return sum(v % 2 for v in collections.Counter(s).values()) < 2
            

    三刷:

    对于unicode还是用HashSet比较好。 题目可以假定Alphabet只有ASCII所以我们也可以用bitmap。

    Java:

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            if (s == null) return false;
            if (s.length() <= 1) return true;
            Set<Character> set = new HashSet<>();
            for (int i = 0; i < s.length(); i++) {
                if (!set.add(s.charAt(i))) set.remove(s.charAt(i));
            }
            return set.size() <= 1;
        }
    }

    Update:

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            if (s == null) return false;
            Set<Character> set = new HashSet<>(s.length());
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (!set.add(c)) set.remove(c);
            }
            return set.size() < 2;
        }
    }

    Reference:

    https://leetcode.com/discuss/71076/5-lines-simple-java-solution-with-explanation

    https://leetcode.com/discuss/70848/3-line-java-functional-declarative-solution

    https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java

  • 相关阅读:
    洛谷P2516 [HAOI2010]最长公共子序列 动态规划 容斥原理
    [LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
    [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
    [LeetCode]129. Sum Root to Leaf Numbers路径数字求和
    [leetcode]645. Set Mismatch
    [leetcode]110BalancedBinaryTree平衡二叉树
    [leetcode]199. Binary Tree Right Side View
    [LeetCode]116. Populating Next Right Pointers in Each Node
    [leetcode]720. Longest Word in Dictionary字典中最长的单词
    [LeetCode]690. Employee Importance员工重要信息
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5021398.html
Copyright © 2020-2023  润新知