# 0266. Palindrome Permutation ###### tags: `Leetcode` `Easy` `FaceBook` `Palindrome` Link: https://leetcode.com/problems/palindrome-permutation/ ## 思路 ### 思路一 计算每个字母出现的频率,如果有两个奇数,就会false ### 思路二 One pass 用set找有几个数出现了奇数次 ## Code ### 思路一 ```java= class Solution { public boolean canPermutePalindrome(String s) { int[] cnt = new int[26]; char[] charArray = s.toCharArray(); for(int i = 0;i < charArray.length;i++){ cnt[charArray[i]-'a']++; } boolean flag = false; for(int i = 0;i < 26;i++){ if(cnt[i]%2!=0){ if(flag){ return false; } else{ flag = true; } } } return true; } } ``` ### 思路二 ```java= class Solution { public boolean canPermutePalindrome(String s) { Set<Character> set = new HashSet<>(); for(int i = 0;i < s.length();i++){ if(set.contains(s.charAt(i))){ set.remove(s.charAt(i)); } else{ set.add(s.charAt(i)); } } return set.size()<=1; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up