Skip to content

Commit 74a2d45

Browse files
committed
Add 1.4 PalinPerm solution
1 parent 477535c commit 74a2d45

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Given a string, write a function to check if it is a permutation of a palindrome
2+
3+
const isPalindromePermutation = (str) => {
4+
const strNoSpaces = str.split(' ').join('');
5+
const obj = {};
6+
var oddCount = 0;
7+
for (let char of strNoSpaces) {
8+
if (obj[char] !== undefined) {
9+
obj[char]++;
10+
} else {
11+
obj[char] = 1;
12+
}
13+
}
14+
for (let key in obj) {
15+
if (obj[key] % 2) {
16+
oddCount++;
17+
}
18+
if (oddCount > 1) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
25+
console.log(isPalindromePermutation('tact coa') === true);
26+
console.log(isPalindromePermutation('tact cooa') === true);
27+
console.log(isPalindromePermutation('tacr coa') === false);
28+
console.log(isPalindromePermutation('tactr coa') === false);

0 commit comments

Comments
 (0)