-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromePermutation.js
55 lines (45 loc) · 1.32 KB
/
palindromePermutation.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// Palindrome Permutation: Given a string, write a function to
/// check if it is a permutation of a palin - drome.A palindrome
/// is a word or phrase that is the same forwards and backwards.A
/// permutation is a rearrangement of letters.The palindrome does not
/// need to be limited to just dictionary words.
function checkPalindromPermutation2(word) {
const letterCount = new Map();
for (let letter of word) {
if (letterCount.has(letter)) {
letterCount.set(letter, letterCount.get(letter) + 1);
} else {
letterCount.set(letter, 1);
}
}
let oddCount = 0;
letterCount.forEach((value, key) => {
if (value % 2 != 0) {
oddCount++;
}
});
return oddCount <= 1;
}
function checkPalindromPermutation(word) {
const letters = word.split("");
const mappedLetters = new Map();
for (var letter of letters) {
if (letter != "" && mappedLetters.has(letter)) {
mappedLetters.set(letter, mappedLetters.get(letter) + 1);
} else {
mappedLetters.set(letter, 1);
}
}
let odd = 0;
for (var mappedLetter of mappedLetters) {
if (letter != "" && mappedLetters.get(letter) % 2 != 0) {
odd++;
}
if (odd > 1) {
return false;
}
}
return true;
}
console.log(checkPalindromPermutation2("tot"));
console.log(checkPalindromPermutation2("car"));