-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome-permutation.cpp
More file actions
33 lines (32 loc) · 904 Bytes
/
palindrome-permutation.cpp
File metadata and controls
33 lines (32 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
// ??????????????
bool canPermutePalindrome1(string s) {
unordered_map<char,int> um;
for(const auto &c:s) um[c]++;
if(s.size()%2==0){
for(auto &n:um){
if(n.second%2==1) return false;
}
}
else{
bool flag=false;
for(auto &n:um){
if(n.second%2==1){
if(flag==true) return false;
flag=true;
}
}
}
return true;
}
// https://leetcode.com/problems/palindrome-permutation/discuss/69582/Java-solution-wSet-one-pass-without-counters.
bool canPermutePalindrome(string s) {
unordered_map<char,int> um;
for(const auto &c:s){
if(um.find(c)!=um.end()) um.erase(c);
else um[c]++;
}
return um.size()<=1;
}
};