Created
January 10, 2019 20:27
-
-
Save nhu313/b5be9a717ae70e5e8cf6b61cbb035d19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.interview; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class PalindromePermutation { | |
public static boolean isPalindromePermutation(String value) { | |
Map<Character, Integer> counts = countCharacters(value); | |
boolean odd = false; | |
for(int count : counts.values()) { | |
if (count % 2 == 1) { | |
if (odd) { | |
return false; | |
} else { | |
odd = true; | |
} | |
} | |
} | |
return true; | |
} | |
private static Map<Character, Integer> countCharacters(String value) { | |
Map<Character, Integer> counts = new HashMap<>(); | |
for (int i = 0; i < value.length(); i++){ | |
Character c = Character.toLowerCase(value.charAt(i)); | |
if (c >= 'a' && c <= 'z') { | |
int count = counts.getOrDefault(c, 0); | |
counts.put(c, count + 1); | |
} | |
} | |
return counts; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment