Permutation Palindrome

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

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

Analyse: use hash map

 1 class Solution {
 2 public:
 3     bool canPermutePalindrome(string s) {
 4         unordered_map<char, int> um;
 5         for (char ch : s)
 6             um[ch]++;
 7
 8         bool canPermute = true;
 9         for (unordered_map<char, int>::iterator ite = um.begin(); ite != um.end(); ite++) {
10             if (ite->second % 2) {
11                 if (!canPermute) return false;
12                 else canPermute = false;
13             }
14         }
15         return true;
16     }
17 };

Analyse: use A[ch]. There are 128 characters in ASCII, we can convert a char to int automatically.

 1 class Solution {
 2 public:
 3     bool canPermutePalindrome(string s) {
 4         vector<bool> charOccurenceBool(128, false);
 5         for (char ch : s)
 6             charOccurenceBool[ch] = !charOccurenceBool[ch];
 7
 8         int count = 0;
 9         for (int i = 0; i < charOccurenceBool.size(); i++) {
10             if (charOccurenceBool[i]) count++;
11         }
12         return count < 2;
13     }
14 };
时间: 2024-10-08 10:04:31

Permutation Palindrome的相关文章

[LeetCode] Longest Palindrome 最长回文串

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the leng

267. 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 = "a

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

Palindrome Permutation -- LeetCode

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. 要点:学习如何iterate一个unordered_map. 1 class Solution { 2 public: 3 bool canP

Palindrome Permutation II 解答

Question 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

266. Palindrome Permutation

/* *266. Palindrome Permutation *2016-6-24 by Mingyang *这个题目很简单的用Hashmap来计算,而高手用的是HashSet来做,遇到一样的,就remove,从没出现的就Add *不过自己的代码还借鉴了如何loop hashmap的value */ public boolean canPermutePalindrome(String s) { int len=s.length(); if(s==null||len==0) return tru

[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

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

Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. 解法一: 解题思路: The idea is to iterate over string, adding current character