Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Last active December 14, 2021 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswomack/34e6d933843a4952f9808df1629d9a81 to your computer and use it in GitHub Desktop.
Save jameswomack/34e6d933843a4952f9808df1629d9a81 to your computer and use it in GitHub Desktop.
Determining if a string is a palindrome—or becomes a palindrome by dropping one of its characters
function isArrayPalindrome (array) {
const forward = [...array].join();
const reversed = [...array].reverse().join();
if (reversed == forward) {
return true;
}
return false;
}
const getArraySansIndex = (a, _index) => a.filter((item, index) => index !== _index);
function doesStringContainPalindrome (string) {
const array = string.split('');
if (isArrayPalindrome(array)) {
return true;
}
let countBound = array.length;
let currentIndex = 0;
while (currentIndex < countBound) {
const arraySansIndex = getArraySansIndex(array, currentIndex);
if (isArrayPalindrome(arraySansIndex)) {
return true;
}
currentIndex++;
}
return false;
}
function determineIfStringsContainPalindromes() {
const stringsToCheckForPalindromes = [
'racecar', // 0
'rbacecar',
'rbacecar',
'racecarb',
'abaaaa',
'racecae', // 5: first indexed string that cannot be turned into a palindrome by dropping a letter
];
const results = stringsToCheckForPalindromes.map(doesStringContainPalindrome);
const indexOfFalse = results.indexOf(false);
console.log(indexOfFalse === 5);
}
// Goal is to have each of the logs print `true`
determineIfStringsContainPalindromes();
function isArrayPalindrome (array) {
const forward = [...array].join();
const reversed = [...array].reverse().join();
if (reversed == forward) {
return true;
}
return false;
}
function doesStringContainPalindrome (string) {
const array = Array.from(string);
if (isArrayPalindrome(array)) {
return true;
}
}
function determineIfStringsContainPalindromes() {
let result;
result = doesStringContainPalindrome('racecar');
console.log(result === true);
result = doesStringContainPalindrome('rbacecar');
console.log(result === true);
result = doesStringContainPalindrome('racecarb');
console.log(result === true);
result = doesStringContainPalindrome('abaaaa');
console.log(result === true);
result = doesStringContainPalindrome('racecae');
console.log(result === false);
}
// Goal is to have each of the logs print `true`
determineIfStringsContainPalindromes();
@jameswomack
Copy link
Author

Screen Shot 2021-09-21 at 2 29 41 PM

@jameswomack
Copy link
Author

Of course, any string w/ 2 or more of the same character could technically be said to contain the possibility of a palindrome. That is why, in this case, "possibility of a palindrome" is defined as "a string in which removing 0-1 characters from any index will result in a string that contains the same character sequence whether read from the left end or right end of said string"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment