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 canPermutePalindrome(string s) {
 4         unordered_map<char, int> dict;
 5         for (int i = 0, n = s.size(); i < n; i++) {
 6             if (dict.count(s[i]) == 0)
 7                 dict.insert(make_pair(s[i], 1));
 8             else dict[s[i]]++;
 9         }
10         int oddCount = 0;
11         for (auto it = dict.begin(); it != dict.end(); it++) {
12             if (it->second % 2) oddCount++;
13             if (oddCount > 1) return false;
14         }
15         return true;
16     }
17 };

 

posted @ 2016-08-15 02:39  fenshen371  阅读(144)  评论(0编辑  收藏  举报