Dasphillipbrau
Dasphillipbrau

Reputation: 582

Can someone please explain this Palindrome solution?

OK, so I'm trying to improve my JS and I've come across the popular Palindrome checker exercise. This time, this solution from freeCodeCamp is supposed to perform very well, but I cannot understand several aspects of it.

/this solution performs at minimum 7x better, at maximum infinitely better.
    //read the explanation for the reason why. I just failed this in an interview.
    function palindrome(str) {
      //assign a front and a back pointer
      let front = 0
      let back = str.length - 1

      //back and front pointers won't always meet in the middle, so use (back > front)
      while (back > front) {
        //increments front pointer if current character doesn't meet criteria
        if ( str[front].match(/[\W_]/) ) {
          front++
          continue
        }
        //decrements back pointer if current character doesn't meet criteria
        if ( str[back].match(/[\W_]/) ) {
          back--
          continue
        }
        //finally does the comparison on the current character
        if ( str[front].toLowerCase() !== str[back].toLowerCase() ) return false
        front++
        back--
      }

      //if the whole string has been compared without returning false, it's a palindrome!
      return true

    }

Now, I have several questions regarding this code:

1- What is the point of reducing the length of str by 1? I've seen this in several Palindrome checkers and I still cannot wrap my head around it.

2- does the .match method means "return true if the argument contains these characters"?

3- how come "/[\W_]/" equals "all characters"?

4- what is the point in adding 1 and subtracting 1 to front and back? Especially at the end of the function.

Thank you and sorry if it's a silly question, I'm just really interested in comprehending the logic here.

Upvotes: 0

Views: 90

Answers (2)

Mark
Mark

Reputation: 92460

This is simulataneously looking at the front of the string and the end of the string and comparing. At each loop it moves the from forward and the back backward:

loop 1
  amanaplanacanalpanama
  |                   |
front                back

front === back? if not it iss not a palindrome

loop 2 (front+1 & back -1)
  amanaplanacanalpanama
   |                 |
 front              back

front === back?

etc.

If this continues to return true, you have a palindrome.

A problem you might run into is a palindrome like:

madam I'm adam

The ' and spaces screw it up, but most people still call it a palindrome. This is why you have the line like:

str[back].match(/[\W_]/)

.match(/[\W_]/) tests whether a letter is a "non-word" letter like ''' or even a space and moves the front an back pointer past it. This allows the test to not care about spaces and non-word characters.

Upvotes: 1

Ryan Karumanchery
Ryan Karumanchery

Reputation: 26

To answer your first question, 'back' is a pointer which will point to the back of the string. The reason why its value is str.length - 1 is because str.length will give the number of attributes in the string, although the index starts at 0. For this reason, you will need to subtract 1 from the length to get the index of the last attribute.

To answer your last question, adding 1 / subtracting 1 from the front/back respectively is to increment the attributes being tested against one another. For example, if the string was "abba", after comparing the first letter with the last letter, it would increment the counter so that the first b (the new 'front') will be compared to the second b (the new 'back')

Upvotes: 1

Related Questions