标签:
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 };
Palindrome Permutation -- LeetCode
标签:
原文地址:http://www.cnblogs.com/fenshen371/p/5771537.html