Created
November 14, 2015 15:03
-
-
Save NKjoep/8c1163e407c63ffcdc07 to your computer and use it in GitHub Desktop.
Javascript Palindromes of Anagrams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns `true` if any of the given string contains a palindrome. | |
* @param {String} s the string to test | |
*/ | |
function PalindromeFinder(s) { | |
//build an array of characters from the string `s` | |
var chars = s.split(''); | |
//store how many of the same char within the string (so the array) | |
var charsOccurences = {}; | |
chars.forEach(function(c) { | |
if (charsOccurences[c] === undefined) { //create if new | |
charsOccurences[c] = 1; | |
} else { //increment | |
charsOccurences[c] = charsOccurences[c] + 1; | |
} | |
}); | |
var evens = 0; //count the evens | |
var odds = 0; //count the odds | |
for (var char in charsOccurences) { | |
var occurrs = charsOccurences[char]; | |
if (occurrs % 2 === 0) { //count evens... | |
evens = evens + 1; | |
} else { //count odds... | |
odds = odds + 1; | |
} | |
} | |
//the result: | |
// if the string length is even then `no odds chars can be present` | |
// if the string length is odd, then `JUST only one odd char can be found`. | |
var result = false; | |
if (s.length % 2 === 0) { | |
if (odds === 0) { //no odds here please... | |
result = true; | |
} | |
} else { | |
if (odds === 1) { //here we want only one odd | |
result = true; | |
} | |
} | |
return result; | |
} | |
Author
NKjoep
commented
Jul 2, 2017
var isPaindrome = function (str) {
str = str.toLowerCase().replace(/[^\w]/g, '');
const length = str.length;
var pivot = Math.floor(length / 2);
for (let i = length - 1; i > pivot; i--) {
let positionTail = i;
let positionHead = (length - 1) - i;
let charFromTail = str[positionTail];
let charFromHead = str[positionHead];
if (charFromTail !== charFromHead) {
// console.log(`${charFromTail}@${positionTail} - ${charFromHead}@${positionHead}`);
return false;
}
}
return true;
}
console.log('test', 'ispal', 'str');
console.log('----', '-----', '----');
console.log(isPaindrome('a') === true,
isPaindrome('a'),
'a');
console.log(isPaindrome('aa') === true,
isPaindrome('aa'),
'aa');
console.log(isPaindrome('aba') === true,
isPaindrome('aba'),
'aba');
console.log(isPaindrome('ab') === true,
isPaindrome('ab'),
'ab');
console.log(isPaindrome('abc') === false,
isPaindrome('abc'),
'abc');
console.log(isPaindrome('123a321') === true,
isPaindrome('123a321'),
'123a321');
console.log(isPaindrome('123321') === true,
isPaindrome('123321'),
'123321');
console.log(isPaindrome('11a11') === true,
isPaindrome('11a11'),
'11a11');
console.log(isPaindrome('1234567890a0987654321') === true,
isPaindrome('1234567890a0987654321'),
'1234567890a0987654321');
console.log(isPaindrome('a;dfk43kl4') === false,
isPaindrome('a;dfk43kl4'),
'a;dfk43kl4');
console.log(isPaindrome('4577kddk223k5') === false,
isPaindrome('4577kddk223k5'),
'4577kddk223k5');
console.log(isPaindrome('A man a plan a canal Panama') === true,
isPaindrome('A man a plan a canal Panama'),
'A man a plan a canal Panama');
console.log(isPaindrome('Madam In Eden, I’m Adam') === true,
isPaindrome('Madam In Eden, I’m Adam'),
'Madam In Eden, I’m Adam');
console.log(isPaindrome('Mr. Owl Ate My Metal Worm') === true,
isPaindrome('Mr. Owl Ate My Metal Worm'),
'Mr. Owl Ate My Metal Worm');
console.log(isPaindrome('A Santa Lived As a Devil At NASA') === true,
isPaindrome('A Santa Lived As a Devil At NASA'),
'A Santa Lived As a Devil At NASA');
console.log(isPaindrome('Dammit, I’m Mad!') === true,
isPaindrome('Dammit, I’m Mad!'),
'Dammit, I’m Mad!');
console.log(isPaindrome('Was It A Rat I Saw') === true,
isPaindrome('Was It A Rat I Saw'),
'Was It A Rat I Saw');
console.log(isPaindrome('Do Geese See God?') === true,
isPaindrome('Do Geese See God?'),
'Do Geese See God?');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment