File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 232232| 261 | 🔒 [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree) | [](src/GraphValidTree.java) | |
233233| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number) | [](src/UglyNumber.java) [](python/ugly_number.py) | |
234234| 264 | [Ugly Number II](https://leetcode.com/problems/ugly-number-ii) | [](src/UglyNumberII.java) | |
235- | 266 | 🔒 [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation) | | |
235+ | 266 | 🔒 [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation) | [](src/PalindromePermutation.java) | |
236236| 267 | 🔒 [Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii) | | |
237237| 268 | [Missing Number](https://leetcode.com/problems/missing-number) | [](src/MissingNumber.java) [](python/missing_number.py) | |
238238| 269 | [Alien Dictionary](https://leetcode.com/problems/alien-dictionary) | [](src/AlienDictionary.java) | |
Original file line number Diff line number Diff line change 1+ // https://leetcode.com/problems/palindrome-permutation
2+ // T: O(|s|)
3+ // S: O(|s|)
4+
5+ import java .util .HashMap ;
6+ import java .util .Map ;
7+
8+ public class PalindromePermutation {
9+ public boolean canPermutePalindrome (String s ) {
10+ final Map <Character , Integer > frequencies = getCharacterFrequencies (s );
11+ return getNumberOfOddChars (frequencies ) <= 1 ;
12+ }
13+
14+ private static Map <Character , Integer > getCharacterFrequencies (String string ) {
15+ final Map <Character , Integer > result = new HashMap <>();
16+ for (int i = 0 ; i < string .length () ; i ++) {
17+ final char c = string .charAt (i );
18+ result .put (c , result .getOrDefault (c , 0 ) + 1 );
19+ }
20+ return result ;
21+ }
22+
23+ private static int getNumberOfOddChars (Map <Character , Integer > frequencies ) {
24+ int result = 0 ;
25+ for (int freq : frequencies .values ()) {
26+ if (freq % 2 == 1 ) {
27+ result ++;
28+ }
29+ }
30+ return result ;
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments