-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_157.js
39 lines (34 loc) Β· 997 Bytes
/
problem_157.js
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
39
/**
* Finds if permutation of a string is a palindrome
* @param {string} str
* @return {boolean}
*/
function isPalindromePerm(str) {
// base case
if (str.length === 1) return false;
const obj = {};
const strArr = str.split('');
let flagCount = 0;
// count occurence of each character
strArr.forEach(char => {
if (!obj[char]) {
obj[char] = 1;
} else {
obj[char] += 1;
}
});
// check number of odd occurences
Object.values(obj).forEach(value => {
if (value % 2 === 1) flagCount += 1;
});
return flagCount <= 1;
}
console.log(isPalindromePerm('carrace')); // true
console.log(isPalindromePerm('daily')); // false
console.log(isPalindromePerm('tacocat')); // true
console.log(isPalindromePerm('pap')); // true
console.log(isPalindromePerm('p')); // false
console.log(isPalindromePerm('pa')); // false
console.log(isPalindromePerm('aa')); // true
console.log(isPalindromePerm('aaa')); // true
console.log(isPalindromePerm('bub')); // true