Palindrome String is a question asked in many interviews including FAANG companies.  


The question for an algorithm is, 


Given a string x, return true if x is a palindrome string.

A string is a palindrome when it reads the same backward as forward.

For example, 'level' is a palindrome while 'level' is not.


Example 1:

Input: x = level

Output: true

Explanation: 'level' reads as 'level' from left to right and from right to left.


Example 2:

Input: x = race

Output: false

Explanation: From left to right, it reads 'race'. From right to left, it becomes 'eacr'. Therefore it is not a palindrome.


Example 3:

Input: x = radar

Output: true

Explanation: Reads radar from right to left. Therefore it is a palindrome.



Let's start with the algorithm,

We will be going to match letter by letter from start to end. Take a example to 'level' and we will be going to match 'l' from the start to the 'l' from the last then go for 'e' then 'v'. 


Here is the algorithm,

var isPalindrome = function(x) {

    x=x.toString();

    var start=0;

    var end=x.length-1;

    var result=true;

    while(end>start)

    {

    if(x[start]!=x[end])

    {

    result=false;

    }

    start++;

    end--;

    }

    return result;

}


Here, we are converting any word to a string first. Then we declared to start with a value 0 and end with the value length of word -1 and result with default true.

In the while loop, we will loop until end becomes less than the start index. Then, we have the condition of matching letters condition if the letter doesn't match then the result becomes false. the start is incrementing and the end is decrementing so that they can match one by one each character from left to right with the right to left of the word.


I hope you understood the algorithm, if you have any doubts please let us know in the comments.