Skip to content

Commit 29b0c1d

Browse files
committed
Refactor 1.4 PalinPerm with ES6 Map solution
1 parent b533d9f commit 29b0c1d

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

JavaScript/chapter01/1.4 - PalinPerm/rroque98_sol.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Given a string, write a function to check if it is a permutation of a palindrome
22

33
const isPalindromePermutation = (str) => {
4-
const strNoSpaces = str.split(' ').join('');
5-
const obj = {};
4+
const strNoSpaces = str.replace(/ /g, '');
65
var oddCount = 0;
6+
const frequencies = new Map();
77
for (let char of strNoSpaces) {
8-
if (obj[char] !== undefined) {
9-
obj[char]++;
10-
} else {
11-
obj[char] = 1;
12-
}
8+
frequencies.set(char, 1 + (frequencies.get(char) || 0));
139
}
14-
for (let key in obj) {
15-
if (obj[key] % 2) {
16-
oddCount++;
10+
for (let frequency of frequencies.values()) {
11+
if (frequency % 2 === 0) {
12+
continue;
1713
}
14+
oddCount++;
1815
if (oddCount > 1) {
1916
return false;
2017
}

0 commit comments

Comments
 (0)