DEV Community

Naya Willis
Naya Willis

Posted on • Edited on

1

IsPalindrome Code Challenge

Hello, everyone. Today I will be walking you through the isPalindrome problem on LeetCode. Your approach may be different, or perhaps even better but don't rub it in, just enjoy. Ok, so let's do this.

The Task

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

const isPalindrome = s => {
    // CODE GOES IN HERE
};
Enter fullscreen mode Exit fullscreen mode

My Approach

I started by first converting the passed in string to lowercase letters to avoid any case sensitivity issues.

s = s.toLowerCase()
Enter fullscreen mode Exit fullscreen mode

Next, I created a string containing letters a-z and number 0-9 to rule out any special characters i.e !?:.

let alphas = "abcdefghijklmnopqrstuvwxyz0123456789"
Enter fullscreen mode Exit fullscreen mode

Next, I created a variable called caughtAlphas which I'm going to use to push all of the alphas into that array, meanwhile ignoring special characters.

let caughtAlphas = []
Enter fullscreen mode Exit fullscreen mode

Then I declared 2 let variables, one that I'll use to convert the caughtAlphas array to a string, and the second one will give me a boolean value based on whether or not the string is a palindrome

let caughtAlphasToString;
let checkIfPalindrome;
Enter fullscreen mode Exit fullscreen mode

To handle the checking of each character in the string, I used a for loop like so...

for (let i = 0; i < s.length; i++) {
     if(alphas.includes(s[i])) { //alphas contain current char?
          caughtAlphas.push(s[i]) //great, so push it in caught alphas
     }
}
Enter fullscreen mode Exit fullscreen mode

Now, to convert the caughtAlphas array into a string...

caughtAlphasToString = caughtAlphas.join('')
Enter fullscreen mode Exit fullscreen mode

Now, checking it against itself reversed then assign it to the checkIfPalindrome variable

checkIfPalindrome = (caughtAlphasToString === caughtAlphasToString.split('').reverse().join(''))
Enter fullscreen mode Exit fullscreen mode

Lastly, returning it

return checkIfPalindrome
Enter fullscreen mode Exit fullscreen mode

So, now if we put it all together we have...

const isPalindrome = s => {
    s = s.toLowerCase()
    let alphas = "abcdefghijklmnopqrstuvwxyz0123456789"
    let caughtAlphas = []
    let caughtAlphasToString;
    let checkIfPalindrome;

    for (let i = 0; i < s.length; i++) {
        if(alphas.includes(s[i])) {
            caughtAlphas.push(s[i])
        }
    }

    caughtAlphasToString = caughtAlphas.join('')
    checkIfPalindrome = (caughtAlphasToString === caughtAlphasToString.split('').reverse().join(''))
    return checkIfPalindrome
}
Enter fullscreen mode Exit fullscreen mode

This challenge was fun. Hope you enjoyed the breakdown. Post your solutions in the comment section.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️