http://www.elvisyu.com/permutation-palindrome/
public boolean canPermutePalindrome(String s) { Set<Character> set=new HashSet<Character>(); for(int i=0; i<s.length(); ++i) if (!set.add(s.charAt(i))) set.remove(s.charAt(i)); return set.size()<=1; }
https://leetcode.com/discuss/71076/5-lines-simple-java-solution-with-explanation
public boolean canPermutePalindrome(String s) { Set<Character>set = new HashSet<Character>(); for (char c : s.toCharArray()) if (set.contains(c)) set.remove(c);// If char already exists in set, then remove it from set else set.add(c);// If char doesn't exists in set, then add it to set return set.size() <= 1; }
https://leetcode.com/discuss/53744/java-ac-8-lines
public boolean canPermutePalindrome(String s) { int odd = 0; int[] arr = new int[256]; for(char c:s.toCharArray()) arr[c]++; for(int x:arr) { if(x%2!=0) { //No worry about even. We only care about how many odds. odd++; if(odd>1) //Should only have one odd. return false; } } return true; }
http://likesky3.iteye.com/blog/2237297
https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java?show=53180
bool canPermutePalindrome(string s) { int odd = 0, counts[256] = {0}; for (char c : s) odd += ++counts[c] & 1 ? 1 : -1; return odd <= 1; }
public boolean canPermutePalindrome(String s) { BitSet bs = new BitSet(); for (byte b : s.getBytes()) bs.flip(b); return bs.cardinality() < 2; }
X.
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_04_Palindrome_Permutation/QuestionC.java
/* Toggle the ith bit in the integer. */
public static int toggle(int bitVector, int index) {
if (index < 0) return bitVector;
int mask = 1 << index;
if ((bitVector & mask) == 0) {
bitVector |= mask;
} else {
bitVector &= ~mask;
}
return bitVector;
}
/* Create bit vector for string. For each letter with value i,
* toggle the ith bit. */
public static int createBitVector(String phrase) {
int bitVector = 0;
for (char c : phrase.toCharArray()) {
int x = Common.getCharNumber(c);
bitVector = toggle(bitVector, x);
}
return bitVector;
}
/* Check that exactly one bit is set by subtracting one from the
* integer and ANDing it with the original integer. */
public static boolean checkExactlyOneBitSet(int bitVector) {
return (bitVector & (bitVector - 1)) == 0;
}
public static boolean isPermutationOfPalindrome(String phrase) {
int bitVector = createBitVector(phrase);
return bitVector == 0 || checkExactlyOneBitSet(bitVector);
}
https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_04_Palindrome_Permutation
public static boolean checkMaxOneOdd(int[] table) {
boolean foundOdd = false;
for (int count : table) {
if (count % 2 == 1) {
if (foundOdd) {
return false;
}
foundOdd = true;
}
}
return true;
}
public static boolean isPermutationOfPalindrome(String phrase) {
int[] table = Common.buildCharFrequencyTable(phrase);
return checkMaxOneOdd(table);
}
public static boolean isPermutationOfPalindrome(String phrase) {
int countOdd = 0;
int[] table = new int[Character.getNumericValue('z') - Character.getNumericValue('a') + 1];
for (char c : phrase.toCharArray()) {
int x = Common.getCharNumber(c);
if (x != -1) {
table[x]++;
if (table[x] % 2 == 1) {
countOdd++;
} else {
countOdd--;
}
}
}
return countOdd <= 1;
}
Read full article from LIKE CODING: LeetCode [266] Palindrome Permutation
Given a string, determine if a permutation of the string could form a palindrome.
For example, "code" -> False, "aab" -> True, "carerac" -> True.
“civic” should return True
“ivicc” should return True
“civil” should return False
“livci” should return False
Hint:
Consider the palindromes of odd vs even length. What difference do you notice? Count the frequency of each character. If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?
HashSet基本思路也就是从左向右扫一遍,如果元素不存在set中,放入。如果存在,拿出来删掉。因为palindrome都是对称的,表示所有元素都是成对出现的(偶数情况,奇数情况下可以有一个元素在正中心),因此在遍历完一遍后,如果Hashset 内有超过1个以上的元素,则不为palindrome,反之则是palindrome。
https://leetcode.com/discuss/53472/simple-java-solution
https://leetcode.com/discuss/53472/simple-java-solution
public boolean checkPermutationPalindrome(String input){
Set hashSet = new HashSet<Character>();
for(int i=0; i<input.length(); i++){
if(hashSet.contains(input.charAt(i)))
hashSet.remove(input.charAt(i));
else
hashSet.add(input.charAt(i));
}
if(hashSet.size() <= 1)
return true;
else
return false;
}
https://leetcode.com/discuss/53295/java-solution-w-set-one-pass-without-counterspublic boolean canPermutePalindrome(String s) { Set<Character> set=new HashSet<Character>(); for(int i=0; i<s.length(); ++i) if (!set.add(s.charAt(i))) set.remove(s.charAt(i)); return set.size()<=1; }
https://leetcode.com/discuss/71076/5-lines-simple-java-solution-with-explanation
public boolean canPermutePalindrome(String s) { Set<Character>set = new HashSet<Character>(); for (char c : s.toCharArray()) if (set.contains(c)) set.remove(c);// If char already exists in set, then remove it from set else set.add(c);// If char doesn't exists in set, then add it to set return set.size() <= 1; }
https://leetcode.com/discuss/53744/java-ac-8-lines
public boolean canPermutePalindrome(String s) { int odd = 0; int[] arr = new int[256]; for(char c:s.toCharArray()) arr[c]++; for(int x:arr) { if(x%2!=0) { //No worry about even. We only care about how many odds. odd++; if(odd>1) //Should only have one odd. return false; } } return true; }
http://likesky3.iteye.com/blog/2237297
- public boolean canPermutePalindrome1(String s) {
- int[] map = new int[256];
- for (int i = 0; i < s.length(); i++) {
- map[s.charAt(i)]++;
- }
- int oddOccurCounter = 0;
- for (int i = 0; i < 256; i++) {
- if (((map[i]) & 1) == 1)
- oddOccurCounter++;
- }
- if ((s.length() & 1) == 1)
- return oddOccurCounter == 1;
- else
- return oddOccurCounter == 0;
- }
public boolean canPermutePalindrome(String s) { if (s == null) throw new IllegalArgumentException("s is null"); int len = s.length(); if (len <= 1) return true; boolean[] odd_flag = new boolean[256]; int odd_count = 0; for (int i = 0; i < len; i++) { char c = s.charAt(i); if (odd_flag[c]) { odd_flag[c] = false; odd_count--; } else{ odd_flag[c] = true; odd_count++; } } if (odd_count >= 2) return false; else return true; }http://buttercola.blogspot.com/2015/08/leetcode-palindrome-permutation.html
public
boolean
canPermutePalindrome(String s) {
if
(s ==
null
|| s.length() <=
1
) {
return
true
;
}
Map<Character, Integer> map =
new
HashMap<Character, Integer>();
for
(
int
i =
0
; i < s.length(); i++) {
char
letter = s.charAt(i);
if
(map.containsKey(letter)) {
int
count = map.get(letter) +
1
;
map.put(letter, count);
}
else
{
map.put(letter,
1
);
}
}
int
tolerance =
0
;
Iterator it = map.entrySet().iterator();
while
(it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
if
((
int
) pair.getValue() %
2
!=
0
) {
tolerance++;
}
}
if
(s.length() %
2
==
0
) {
return
tolerance ==
0
;
}
else
{
return
tolerance ==
1
;
}
}
bool canPermutePalindrome(string s) { int odd = 0, counts[256] = {0}; for (char c : s) odd += ++counts[c] & 1 ? 1 : -1; return odd <= 1; }
public boolean canPermutePalindrome(String s) { BitSet bs = new BitSet(); for (byte b : s.getBytes()) bs.flip(b); return bs.cardinality() < 2; }
X.
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_04_Palindrome_Permutation/QuestionC.java
/* Toggle the ith bit in the integer. */
public static int toggle(int bitVector, int index) {
if (index < 0) return bitVector;
int mask = 1 << index;
if ((bitVector & mask) == 0) {
bitVector |= mask;
} else {
bitVector &= ~mask;
}
return bitVector;
}
/* Create bit vector for string. For each letter with value i,
* toggle the ith bit. */
public static int createBitVector(String phrase) {
int bitVector = 0;
for (char c : phrase.toCharArray()) {
int x = Common.getCharNumber(c);
bitVector = toggle(bitVector, x);
}
return bitVector;
}
/* Check that exactly one bit is set by subtracting one from the
* integer and ANDing it with the original integer. */
public static boolean checkExactlyOneBitSet(int bitVector) {
return (bitVector & (bitVector - 1)) == 0;
}
public static boolean isPermutationOfPalindrome(String phrase) {
int bitVector = createBitVector(phrase);
return bitVector == 0 || checkExactlyOneBitSet(bitVector);
}
https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_04_Palindrome_Permutation
public static boolean checkMaxOneOdd(int[] table) {
boolean foundOdd = false;
for (int count : table) {
if (count % 2 == 1) {
if (foundOdd) {
return false;
}
foundOdd = true;
}
}
return true;
}
public static boolean isPermutationOfPalindrome(String phrase) {
int[] table = Common.buildCharFrequencyTable(phrase);
return checkMaxOneOdd(table);
}
int countOdd = 0;
int[] table = new int[Character.getNumericValue('z') - Character.getNumericValue('a') + 1];
for (char c : phrase.toCharArray()) {
int x = Common.getCharNumber(c);
if (x != -1) {
table[x]++;
if (table[x] % 2 == 1) {
countOdd++;
} else {
countOdd--;
}
}
}
return countOdd <= 1;
}
Read full article from LIKE CODING: LeetCode [266] Palindrome Permutation