-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromePermutation.js
More file actions
39 lines (33 loc) · 1.13 KB
/
palindromePermutation.js
File metadata and controls
39 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const assert = require('assert');
/*
receives a string and returns if it is possible
make a palidrome of some pemutation of the string
my idea was: if str.length is even all char has to be
a quantity even, if str.length is odd, must have one char
with a odd quantity, the others must be even.
*/
function hasPalindromePermutation(str){
let data = {};
for(let c of str){
data[c] = data[c] + 1 || 1;
}
//test if all chars has quantity even
if(str.length % 2 === 0){
for(let key in data)
if(data[key] % 2 != 0) return false;
}else{
let odds = 0;
for(let key in data){
if(data[key] % 2 != 0) odds++;
if(odds > 1) return false;
}
}
return true;
}
assert.equal(hasPalindromePermutation('ABA'), true);
assert.equal(hasPalindromePermutation('ABC'), false);
assert.equal(hasPalindromePermutation('x'), true);
assert.equal(hasPalindromePermutation('xx'), true);
assert.equal(hasPalindromePermutation('yyy'), true);
assert.equal(hasPalindromePermutation('AABBCC'), true);
assert.equal(hasPalindromePermutation('ABCC'), false);