zoukankan      html  css  js  c++  java
  • [LeetCode] 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?

    给一个字符串,判断它的全排列中是否有回文。

    解法:基本原理就是,如果字符串长度为单数,那么可以有一个字符是单数,其它出现的字符必须都是双数。如果字符串长度为偶数,那么所有的字符都必须是双数,才能组成回文。

    具体实现方法可以有多种。

     Java:

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            // 统计每个字符的个数
            for(int i = 0; i < s.length(); i++){
                char c = s.charAt(i);
                Integer cnt = map.get(c);
                if(cnt == null){
                    cnt = new Integer(0);
                }
                map.put(c, ++cnt);
            }
            // 判断是否只有不大于一个的奇数次字符
            boolean hasOdd = false;
            for(Character c : map.keySet()){
                if(map.get(c) % 2 == 1){
                    if(!hasOdd){
                        hasOdd = true;
                    } else {
                        return false;
                    }
                }
            }
            return true;
        }
    }
    

    Java:

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            Set<Character> set = new HashSet<Character>();
            for(int i = 0; i < s.length(); i++){
                // 出现的第偶数次,将其从Set中移出
                if(set.contains(s.charAt(i))){
                    set.remove(s.charAt(i));
                } else {
                // 出现的第奇数次,将其加入Set中
                    set.add(s.charAt(i));
                }
            }
            // 最多只能有一个奇数次字符
            return set.size() <= 1;
        }
    } 

    Java:

    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;
        }
    }  

    Python:

    class Solution(object):
        def canPermutePalindrome(self, s):
            """
            :type s: str
            :rtype: bool
            """
            d = {}
            for i in s:
                d[i] = d.get(i, 0) + 1
    
            count = 0
            for i in d.values():
                if i % 2 != 0:
                    count += 1
                if count > 1:
                    return False
                    
            return True
    

    Python:

    class Solution(object):
        def canPermutePalindrome(self, s):
            """
            :type s: str
            :rtype: bool
            """
            oddChars = set()
    
            for c in s:
                if c in oddChars:
                    oddChars.remove(c)
                else:
                    oddChars.add(c)
    
            return len(oddChars) <= 1  

    Python:

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

    C++:

    class Solution {
    public:
        bool canPermutePalindrome(string s) {
            bitset<256> bits;
            for (const auto& c : s) {
                bits.flip(c);
            }
            return bits.count() < 2;
        }
    };
    

    C++:

    class Solution {
    public:
        bool canPermutePalindrome(string s) {
            unordered_map<char, int> m;
            int cnt = 0;
            for (auto a : s) ++m[a];
            for (auto it = m.begin(); it != m.end(); ++it) {
                if (it->second % 2) ++cnt;
            }
            return cnt == 0 || (s.size() % 2 == 1 && cnt == 1);
        }
    };
    

    C++:

    class Solution {
    public:
        bool canPermutePalindrome(string s) {
            set<char> t;
            for (auto a : s) {
                if (t.find(a) == t.end()) t.insert(a);
                else t.erase(a);
            }
            return t.empty() || t.size() == 1;
        }
    };
    

      

    类似题目:

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

       

    All LeetCode Questions List 题目汇总

  • 相关阅读:
    mysql字段使用timestamp类型和默认值CURRENT_TIMESTAMP表示默认为时间,类似mssql的getdate()功能;字段自增
    xmlInitParser 和 xmlCleanupParser 使用详解
    修改虚拟机硬盘的大小
    linux 查看系统进程命令
    linux查看系统信息的命令
    Eclipse快捷键大全(转载)
    获取可执行文件的全路径和所属目录
    dlopen加载c++ 函数及类
    使用LVM在vmware中增大linux的根分区
    查看linux的文件系统是什么格式的(如ext2,ext3,xfs等)?
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8629443.html
Copyright © 2011-2022 走看看