Coding for Scrubs: Anagram Palindrome (JS)

Aymes S.
JavaScript in Plain English
4 min readFeb 16, 2021

--

Welcome Back to ‘Coding for Scrubs’ Series, PART 2!

Hey there, fellow coding enthusiasts! It’s your friendly neighborhood scrub here again, ready to dive into another brain-teasing coding challenge. If you’re getting a kick out of these articles, don’t forget to share the love — share, clap, and follow! 😄

Today’s Topic: Anagram Palindromes in JavaScript.

Ever stumbled upon a coding problem in a tech interview and thought, “Wow, this is a head-scratcher”? That’s exactly what happened to me with this anagram palindrome challenge. So, let’s unravel this mystery together!

1. What is an anagram palindrome?

An anagram is a word or phrase formed by rearranging the letters of another word or phrase, like how ‘silent’ turns into ‘listen’. A palindrome, on the other hand, is a word or phrase that reads the same forwards and backwards. Mash these concepts together, and you’ve got an anagram palindrome — a jumbled word or phrase that can be rearranged into a palindrome. Take ‘yaakk’ for example — it’s a jumble that can be rearranged into ‘kayak’.

2. Pseudocode

Understanding what an anagram palindrome is sets the stage for our solution approach. Recall that a regular palindrome features letters in pairs, except possibly one. In a string with even length, every letter pairs up, while in an odd-length string, one unpaired letter is permissible. In standard palindrome checks, we compare the start and end letters of the string. However, this method won’t work with scrambled words in anagram palindromes.

To tackle this, we’ll employ a new strategy involving an array. This array will serve as a collector for each unique letter from our input string. As we iterate through the string, we’ll use this array to track unpaired letters. Essentially, it becomes a repository for letters without a matching pair. Our logic kicks in here: if a letter from the string is already in our array, it means we’ve found its pair, so we remove it from the array. If it’s not, we add it, signifying an unmatched letter.

Once we’ve examined each letter, the array’s length becomes crucial. For the word to be an anagram palindrome, this array must contain fewer than two elements. If it holds two or more, it means there are too many unmatched letters, and we cannot form a palindrome, thus returning false.

Here’s our game plan:

  1. Create an array to store letters without their pairs.
  2. Iterate through the word with a for loop.
  3. Use a conditional statement to manage our array: if a letter from our string is already in the array, we’ll remove it. If not, we’ll add it.
  4. After the loop, we’ll check our array’s length. If it’s less than 2, we’ve got ourselves an anagram palindrome!

3. Create the Function

Let’s go ahead and follow our pseudocode!

First off, we set up our array and loop through the string:

function isAnagramPalindrome(string) {
let array = []; // Our handy array
for(let i = 0; i < string.length; i++) {
let letter = string[i]; // Current letter
// Time for some array magic
}
}

Next, we’ll deal with the letters in our array:

We are going to use .splice() which changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

We are then going to want to find where that letter is in our array and remove that. So, we will need to use .indexOf() to find the letter.

if(array.includes(letter)) { // Is the letter already in the array?
array.splice(array.indexOf(letter), 1); // Yes? Remove it
}
else {
array.push(letter); // No? Add it
}

And finally, the grand finale:

return array.length < 2; // True for anagram palindrome, false otherwise

Conclusion

And boom! We did it! We’ve just cracked another coding challenge. Stay tuned for more adventures in coding. Until next time, happy coding! 🚀

function isAnagramPalindrome(string) {
let array = [];
for(let i = 0; i < string.length; i++){
let letter = string[i];
if(array.includes(letter)) {
array.splice(array.indexOf(letter), 1);
} else {
array.push(letter);
}
}
return array.length < 2;
}

--

--